Quantcast
Viewing all articles
Browse latest Browse all 41

Answer by Just code for React check children input field value

So, you want to get values on button click you need to identify each input and set them in state, so we will use controlled component

<Input onChangeOfInput={this.onChangeOfInput} placeholder="Title 1" name="Title1" /><br/><Input placeholder="Title 2" name="Title2" onChangeOfInput={this.onChangeOfInput} /><br/><Input placeholder="Title 3" name="Title3" onChangeOfInput={this.onChangeOfInput}/><br/><Input placeholder="Title 4" name="Title4" onChangeOfInput={this.onChangeOfInput}/><br/><button onClick={this.getValues}>Get value</button>

here name is unique key

add one function to parent

  onChangeOfInput =(name,value) =>{    this.setState({            [name]: value        });  }

callback from the child

handleChange(e) {              this.setState({            inputvalue: e.target.value        });       this.props.onChangeOfInput(this.props.name,e.target.value)}

and use the state when you want

 getValues = () =>{    console.log(this.state);  }

demo


Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>