Post using forms on Reactjs
I would like to POST to reactjs but having trouble changing and storing state. this is my state. I am thinking that I GET the "data" and POST it using the format of "post"
I have gone through several articles but I can't seem to find how would I use the POST function to update this.state.post and I need help on what to do when writing the handleSubmit and selectOption functions. I was thinking that whenever the user chooses an option, the state would be updated and the updated state will be posted using the handleSubmit function.
this.state = {
"data": [
{
"id": 1,
"question": "Gender",
"label": "Gender",
"options": [
"Male",
"Female",
"Others"
],
},
],
"post": [
{
question_id: "",
label: "",
relationship: "",
values:
}
]
}
componentDidMount() {
fetch('https://zexample.com/testuser/profile').then(response => {
return response.json()
}).then(jsonify => {
this.setState(jsonify);
console.log(this.state);
});
}
selectOption(id) {
console.log(id);
// this.setState({post: response.data.title})
};
handleSubmit(event) {
event.preventDefault();
console.log('data: ', event);
}
Here is the form that I render using the map() function
render() {
return (
<form onSubmit={this.handleSubmit}>
<div>
{this.state.data.map(question => (
<div key={question.id}>
<span className="icon">Icon</span>
<div>
<label htmlFor={question.id}>
{question.question}
</label>
<select>
{question.options.map(option => (
<option value={option} onChange={this.selectOption.bind(this)}>
{option}
</option>
)
)}
</select>
</div>
</div>
)
)}
<div>
<button type="submit">Continue</button>
</div>
</div>
</form>
);
}
reactjs
add a comment |
I would like to POST to reactjs but having trouble changing and storing state. this is my state. I am thinking that I GET the "data" and POST it using the format of "post"
I have gone through several articles but I can't seem to find how would I use the POST function to update this.state.post and I need help on what to do when writing the handleSubmit and selectOption functions. I was thinking that whenever the user chooses an option, the state would be updated and the updated state will be posted using the handleSubmit function.
this.state = {
"data": [
{
"id": 1,
"question": "Gender",
"label": "Gender",
"options": [
"Male",
"Female",
"Others"
],
},
],
"post": [
{
question_id: "",
label: "",
relationship: "",
values:
}
]
}
componentDidMount() {
fetch('https://zexample.com/testuser/profile').then(response => {
return response.json()
}).then(jsonify => {
this.setState(jsonify);
console.log(this.state);
});
}
selectOption(id) {
console.log(id);
// this.setState({post: response.data.title})
};
handleSubmit(event) {
event.preventDefault();
console.log('data: ', event);
}
Here is the form that I render using the map() function
render() {
return (
<form onSubmit={this.handleSubmit}>
<div>
{this.state.data.map(question => (
<div key={question.id}>
<span className="icon">Icon</span>
<div>
<label htmlFor={question.id}>
{question.question}
</label>
<select>
{question.options.map(option => (
<option value={option} onChange={this.selectOption.bind(this)}>
{option}
</option>
)
)}
</select>
</div>
</div>
)
)}
<div>
<button type="submit">Continue</button>
</div>
</div>
</form>
);
}
reactjs
Kindly post full code.
– Sagar
Nov 28 '18 at 4:52
@sagar: I added the code
– William Tekimam
Nov 28 '18 at 4:57
What is the issue ?
– Sagar Jajoriya
Nov 28 '18 at 5:13
I am having trouble on how to change the state accurately using map because I need to put different answers in different question and then use POST
– William Tekimam
Nov 28 '18 at 5:31
add a comment |
I would like to POST to reactjs but having trouble changing and storing state. this is my state. I am thinking that I GET the "data" and POST it using the format of "post"
I have gone through several articles but I can't seem to find how would I use the POST function to update this.state.post and I need help on what to do when writing the handleSubmit and selectOption functions. I was thinking that whenever the user chooses an option, the state would be updated and the updated state will be posted using the handleSubmit function.
this.state = {
"data": [
{
"id": 1,
"question": "Gender",
"label": "Gender",
"options": [
"Male",
"Female",
"Others"
],
},
],
"post": [
{
question_id: "",
label: "",
relationship: "",
values:
}
]
}
componentDidMount() {
fetch('https://zexample.com/testuser/profile').then(response => {
return response.json()
}).then(jsonify => {
this.setState(jsonify);
console.log(this.state);
});
}
selectOption(id) {
console.log(id);
// this.setState({post: response.data.title})
};
handleSubmit(event) {
event.preventDefault();
console.log('data: ', event);
}
Here is the form that I render using the map() function
render() {
return (
<form onSubmit={this.handleSubmit}>
<div>
{this.state.data.map(question => (
<div key={question.id}>
<span className="icon">Icon</span>
<div>
<label htmlFor={question.id}>
{question.question}
</label>
<select>
{question.options.map(option => (
<option value={option} onChange={this.selectOption.bind(this)}>
{option}
</option>
)
)}
</select>
</div>
</div>
)
)}
<div>
<button type="submit">Continue</button>
</div>
</div>
</form>
);
}
reactjs
I would like to POST to reactjs but having trouble changing and storing state. this is my state. I am thinking that I GET the "data" and POST it using the format of "post"
I have gone through several articles but I can't seem to find how would I use the POST function to update this.state.post and I need help on what to do when writing the handleSubmit and selectOption functions. I was thinking that whenever the user chooses an option, the state would be updated and the updated state will be posted using the handleSubmit function.
this.state = {
"data": [
{
"id": 1,
"question": "Gender",
"label": "Gender",
"options": [
"Male",
"Female",
"Others"
],
},
],
"post": [
{
question_id: "",
label: "",
relationship: "",
values:
}
]
}
componentDidMount() {
fetch('https://zexample.com/testuser/profile').then(response => {
return response.json()
}).then(jsonify => {
this.setState(jsonify);
console.log(this.state);
});
}
selectOption(id) {
console.log(id);
// this.setState({post: response.data.title})
};
handleSubmit(event) {
event.preventDefault();
console.log('data: ', event);
}
Here is the form that I render using the map() function
render() {
return (
<form onSubmit={this.handleSubmit}>
<div>
{this.state.data.map(question => (
<div key={question.id}>
<span className="icon">Icon</span>
<div>
<label htmlFor={question.id}>
{question.question}
</label>
<select>
{question.options.map(option => (
<option value={option} onChange={this.selectOption.bind(this)}>
{option}
</option>
)
)}
</select>
</div>
</div>
)
)}
<div>
<button type="submit">Continue</button>
</div>
</div>
</form>
);
}
reactjs
reactjs
edited Nov 28 '18 at 4:57
William Tekimam
asked Nov 28 '18 at 4:45
William TekimamWilliam Tekimam
396
396
Kindly post full code.
– Sagar
Nov 28 '18 at 4:52
@sagar: I added the code
– William Tekimam
Nov 28 '18 at 4:57
What is the issue ?
– Sagar Jajoriya
Nov 28 '18 at 5:13
I am having trouble on how to change the state accurately using map because I need to put different answers in different question and then use POST
– William Tekimam
Nov 28 '18 at 5:31
add a comment |
Kindly post full code.
– Sagar
Nov 28 '18 at 4:52
@sagar: I added the code
– William Tekimam
Nov 28 '18 at 4:57
What is the issue ?
– Sagar Jajoriya
Nov 28 '18 at 5:13
I am having trouble on how to change the state accurately using map because I need to put different answers in different question and then use POST
– William Tekimam
Nov 28 '18 at 5:31
Kindly post full code.
– Sagar
Nov 28 '18 at 4:52
Kindly post full code.
– Sagar
Nov 28 '18 at 4:52
@sagar: I added the code
– William Tekimam
Nov 28 '18 at 4:57
@sagar: I added the code
– William Tekimam
Nov 28 '18 at 4:57
What is the issue ?
– Sagar Jajoriya
Nov 28 '18 at 5:13
What is the issue ?
– Sagar Jajoriya
Nov 28 '18 at 5:13
I am having trouble on how to change the state accurately using map because I need to put different answers in different question and then use POST
– William Tekimam
Nov 28 '18 at 5:31
I am having trouble on how to change the state accurately using map because I need to put different answers in different question and then use POST
– William Tekimam
Nov 28 '18 at 5:31
add a comment |
1 Answer
1
active
oldest
votes
In order to update state or handle user selected options, u need to use this.setState({...}) react API instead of using POST function (https://reactjs.org/docs/react-component.html)
selectOption=(event)=>{
this.setState((currentState)=>{
return({ ...currentState, options:[event.target.value]})
})
}
handleSubmit should be like this:-
handleSubmit=()=>{
axios.post('http://localhost:3001/posts/',{...this.state})
.then()
.catch()
};
}
Here i am assuming payload for post request as this.state object...I am showing example of axios library, its upto personal preference if u want to use axios or fetch API for ajax calls...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512307%2fpost-using-forms-on-reactjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In order to update state or handle user selected options, u need to use this.setState({...}) react API instead of using POST function (https://reactjs.org/docs/react-component.html)
selectOption=(event)=>{
this.setState((currentState)=>{
return({ ...currentState, options:[event.target.value]})
})
}
handleSubmit should be like this:-
handleSubmit=()=>{
axios.post('http://localhost:3001/posts/',{...this.state})
.then()
.catch()
};
}
Here i am assuming payload for post request as this.state object...I am showing example of axios library, its upto personal preference if u want to use axios or fetch API for ajax calls...
add a comment |
In order to update state or handle user selected options, u need to use this.setState({...}) react API instead of using POST function (https://reactjs.org/docs/react-component.html)
selectOption=(event)=>{
this.setState((currentState)=>{
return({ ...currentState, options:[event.target.value]})
})
}
handleSubmit should be like this:-
handleSubmit=()=>{
axios.post('http://localhost:3001/posts/',{...this.state})
.then()
.catch()
};
}
Here i am assuming payload for post request as this.state object...I am showing example of axios library, its upto personal preference if u want to use axios or fetch API for ajax calls...
add a comment |
In order to update state or handle user selected options, u need to use this.setState({...}) react API instead of using POST function (https://reactjs.org/docs/react-component.html)
selectOption=(event)=>{
this.setState((currentState)=>{
return({ ...currentState, options:[event.target.value]})
})
}
handleSubmit should be like this:-
handleSubmit=()=>{
axios.post('http://localhost:3001/posts/',{...this.state})
.then()
.catch()
};
}
Here i am assuming payload for post request as this.state object...I am showing example of axios library, its upto personal preference if u want to use axios or fetch API for ajax calls...
In order to update state or handle user selected options, u need to use this.setState({...}) react API instead of using POST function (https://reactjs.org/docs/react-component.html)
selectOption=(event)=>{
this.setState((currentState)=>{
return({ ...currentState, options:[event.target.value]})
})
}
handleSubmit should be like this:-
handleSubmit=()=>{
axios.post('http://localhost:3001/posts/',{...this.state})
.then()
.catch()
};
}
Here i am assuming payload for post request as this.state object...I am showing example of axios library, its upto personal preference if u want to use axios or fetch API for ajax calls...
answered Nov 28 '18 at 5:36
Manpreet SinghManpreet Singh
687
687
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512307%2fpost-using-forms-on-reactjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Kindly post full code.
– Sagar
Nov 28 '18 at 4:52
@sagar: I added the code
– William Tekimam
Nov 28 '18 at 4:57
What is the issue ?
– Sagar Jajoriya
Nov 28 '18 at 5:13
I am having trouble on how to change the state accurately using map because I need to put different answers in different question and then use POST
– William Tekimam
Nov 28 '18 at 5:31