A Pen by harikrishnan on CodePen.
Forked from anonymous/how-to-use-react-refs.markdown
Created
October 23, 2017 13:07
-
-
Save hazkaz/c8f6a62819d0288712e0efd12056c15f to your computer and use it in GitHub Desktop.
How to use React 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
<div id="root"> | |
</div> |
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
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")); |
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
<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/[email protected]/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