Hi guys i need help how to make the editchange will not go through all the input and make changes to it.
When i typed an input inside one of the textbox it repeats to other inputs. Im new to react so i having trouble dealing with this.
the map is on loop,
for example it displays three from api and 3 inputs will appear.
See the picture, hope you can help me.
handleSubmitReply(event, discussionid, classid){
event.preventDefault();
let userid = sessionStorage.getItem('userid');
const {
userAccountId,
reply
} = this.state;
const postData = {
userAccountId:userid,
reply
};
let sessionToken = sessionStorage.getItem('session');
let sessToken = sessionToken.replace(/\"/g, "");
fetch('http://tfismartasp-001-site10.btempurl.com/api/Class/'+ classid +'/discussion/'+discussionid+'/response', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer' + " " + sessToken
},
body: JSON.stringify(postData),
})
.then(response => {
if(response.status === 400){
return response.json();
}else{
this.addNotification('success', 'Success', 'All Data is Saved', 'top-right')
this.componentDidMount();
return response.json();
}
})
.then(responseData => {
console.log(responseData);
return responseData;
})
.catch(err => {
console.log("fetch error" + err);
});
}
this.handleChange = this.handleChange.bind(this);
handleChange(event) {
event.preventDefault();
console.log(event.target.name)
console.log(event.target.value)
this.setState({
[event.target.name]: event.target.value
});
};
{discussionData.map(dd => (
<input type="text" class="form-control bg-light" placeholder="Reply" name={dd.discussion.instructions} value={this.state[dd.discussion.instructions]} onChange={this.handleChange} />
))}
here is the updated code, what will i do to the postData "reply" when i submit it. it requires to fill all the fields.
How to serialize it to be posted in my API
name="reply", soevent.target.namewill always be"reply", s that's all you'll ever set in state. You'll need to give both theinputs and the state properties unique names based onitem. You haven't shown us whatitemscontains, so it's hard to be more specific, but for instance if eachitemhad anameproperty, you'd usename={item.name} value={this.state[item.name]}and then yourhandleChangewould update it correctly.