Created
April 15, 2020 06:56
-
-
Save arunkumar413/3a898aaadea07c8e204cf06e1d1858b9 to your computer and use it in GitHub Desktop.
Lifting reactjs state using hooks
This file contains 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, { useState } from "react"; | |
export default function Child(props) { | |
const handleChange = event => { | |
props.onChange(event.target.value); | |
}; | |
return ( | |
<div> | |
<input value={props.value} onChange={handleChange} /> | |
</div> | |
); | |
} |
This file contains 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, { useState, useEffect } from "react"; | |
import Child from "./Child"; | |
export default function Parent(props) { | |
const [value, setValue] = useState(""); | |
const handleChange = newVal => { | |
setValue(newVal); | |
}; | |
return ( | |
<div className="App"> | |
<h2> {value} </h2> | |
<Child value={value} onChange={handleChange} />; | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment