Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hazkaz/c8f6a62819d0288712e0efd12056c15f to your computer and use it in GitHub Desktop.
Save hazkaz/c8f6a62819d0288712e0efd12056c15f to your computer and use it in GitHub Desktop.
How to use React Refs
<div id="root">
</div>
class Blocker extends React.Component{
constructor(props){
super(props);
this.state={value:null}
}
componentDidMount(){
this.name.value="hari"
this.name.oninput=this.handlechange.bind(this);
}
handlechange(event){
this.setState({value:event.target.value})
}
render(){
return(
<div>
{/*Attaching a ref the input DOM node*/}
<input ref={(input)=>this.name=input} type="text"/>
{this.state.value}
</div>
)
}
}
ReactDOM.render(<Blocker/>,document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
<script src="https://unpkg.com/react-router-dom@4.0.0/umd/react-router-dom.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment