Created
March 27, 2021 16:00
-
-
Save freakflames29/fad2faf8c74c27dfe583659ce1e01dad to your computer and use it in GitHub Desktop.
Two way data binding in react
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
const [nameS,setName]=useState({ | |
names:[ | |
{name:'sourav',age:30}, | |
{name:'Toton',age:50}, | |
{name:'Rick grimes',age:10} | |
], | |
btn:"Switch all names" | |
}) | |
// the function which is called when the input value is changed | |
const twoWay=(event)=> | |
{ | |
setName({ | |
names:[ | |
{name:event.target.value,age:35}, | |
{name:'Toton',age:50}, | |
{name:'Rick grimes',age:10} | |
], | |
btn:"Name switched!" | |
}) | |
} | |
// passing the function as a props to NewComp named component | |
<NewComp chan={twoWay}/> | |
// NewComp.js is the stateless component | |
<input type='text' onChange={props.chan} /> // calling the funtion in onChange evnt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment