Answer the question
In order to leave comments, you need to log in
Catching child component change?
I have a parent component
class Parent extends React.Component {
constructor(props) {
super(props)
this.testInputValue = ''
}
log_test_input_value = () => {
console.log(this.testInputValue);
}
render() {
return (
<div>
<button onClick={log_test_input_value}>Test log</button>
<Child/>
</div>
)
}
}
class Child extends React.Component {
constructor() {
super();
this.testInputValue = "" ; //пусть инпут изначально пустой
}
handleChange(event) {
this.testInput = event.target.value
}
render() {
<input onChange={this.handleChange(e)} placeholder="test" />
}
}
}
Answer the question
In order to leave comments, you need to log in
Store state in the parent component and pass methods in props to the child component.
class Parent extends React.Component {
constructor(props) {
super(props)
this.testInputValue = ''
}
handleChange(event) {
this.testInputValue = event.target.value
}
log_test_input_value = () => {
console.log(this.testInputValue);
}
render() {
return (
<div>
<button onClick={log_test_input_value}>Test log</button>
<Child onChange={ this.handleChange }/>
</div>
)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question