0

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,enter image description here 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

3
  • You need to set unique name to your inputs so that their state can be managed separately. Commented Apr 14, 2020 at 11:21
  • 3
    All of your inputs have name="reply", so event.target.name will always be "reply", s that's all you'll ever set in state. You'll need to give both the inputs and the state properties unique names based on item. You haven't shown us what items contains, so it's hard to be more specific, but for instance if each item had a name property, you'd use name={item.name} value={this.state[item.name]} and then your handleChange would update it correctly. Commented Apr 14, 2020 at 11:21
  • Hi @T.J.Crowder i updated my code your suggestion works. I have another problem after its in the post thanks Commented Apr 14, 2020 at 11:39

2 Answers 2

1

event.target.name will always be "reply" as every one of your inputs has name="reply".

Giving every input a unique name should fix your problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Try Something like that

handleChange(event, i) {
                event.preventDefault();
                console.log(event.target.name)
                console.log(event.target.value)
                this.setState({
                    [`${event.target.name}{index}`]: event.target.value
                });
            };


{items.map((item, index)=> (
  <input type="text" class="form-control bg-light" placeholder="Reply" name=`name${index}` value= 
  {this.state.reply} onChange={e => this.handleChange(e, index)} />
))}

And create initial state as an array. May be this will help you.

Thanks

2 Comments

Hi @Bhatti i did this name={item.name} value={this.state[item.name]}. Im on POST data now. What will be the postData to serialize the input? this is my data postData = { userAccountId:userid, reply }; reply part
this.state should have all the values. Just console.log(this.state) and analyse data and see what you want and send it in a post request. Something like that {reply: JSON.stringify(this.state)}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.