Skip to content

Instantly share code, notes, and snippets.

@freakflames29
Created March 27, 2021 16:00
Show Gist options
  • Save freakflames29/fad2faf8c74c27dfe583659ce1e01dad to your computer and use it in GitHub Desktop.
Save freakflames29/fad2faf8c74c27dfe583659ce1e01dad to your computer and use it in GitHub Desktop.
Two way data binding in react
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