0

Hi I have a problem in react user input text. I have a list of items together with an input text. When i typed inside the first input, all input boxes changes. how to deal with this.

example code

handleEditChange(event) {
        event.preventDefault();
        console.log(event.target.name)
        console.log(event.target.value)
        this.setState({
            [event.target.name]: event.target.value
        });
    };


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

So the input box will repeat based on the loop, for example it will be 3 input boxes. picture sample

enter image description here

How can i prevent this? thanks for help

2 Answers 2

1

Could you use <input ... name={item.name} value={this.state[item.name]}> or something like that?

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

Comments

0

You need to have separate state variables for each input value. You're currently looping through your items and rendering an identical input for each, with the same value being this.state.reply.

According to the picture you attached, you could wrap each input in a Discussion component which would take care of handling only one input at a time.

1 Comment

how can i do that, im having trouble in my setState when i change the name of the name={"reply" + item.id}

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.