Last active
July 11, 2018 11:36
-
-
Save muhammadghazali/2269de49fba26b12df53eb5ef3474074 to your computer and use it in GitHub Desktop.
Stateless function components cannot have refs. https://muhammadghazali.wordpress.com/2018/07/11/stateless-function-components-cannot-have-refs/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
class App extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
input1: "" | |
}; | |
this.handleChange = this.handleChange.bind(this); | |
} | |
handleChange() { | |
this.setState({ input1: this.refs.input1.value }); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>{this.state.text}</h1> | |
<InputWidget update={this.handleChange} />{" "} | |
<span>{this.state.input1}</span> | |
</div> | |
); | |
} | |
} | |
const InputWidget = props => ( | |
<input ref="input" onDoubleClick={props.update} onChange={props.update} /> | |
); | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
class App extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
input1: "", | |
input2: "" | |
}; | |
this.handleChange = this.handleChange.bind(this); | |
} | |
handleChange() { | |
this.setState({ | |
input1: this.input1.refs.input.value, | |
input2: this.input2.refs.input.value | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>{this.state.text}</h1> | |
<InputWidget | |
ref={component => (this.input1 = component)} | |
update={this.handleChange} | |
/>{" "} | |
<span>{this.state.input1}</span> | |
<InputWidget | |
ref={component => (this.input2 = component)} | |
update={this.handleChange} | |
/>{" "} | |
<span>{this.state.input2}</span> | |
</div> | |
); | |
} | |
} | |
class InputWidget extends Component { | |
render() { | |
return ( | |
<input | |
ref="input" | |
onDoubleClick={this.props.update} | |
onChange={this.props.update} | |
/> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment