Last active
March 17, 2020 00:25
-
-
Save allenwei/ab78ea149de2fc18af90a1d10aa3ed05 to your computer and use it in GitHub Desktop.
useState with props Wrong way
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 from "react"; | |
const Box = ({ num }) => { | |
const [state, setState] = React.useState(num); | |
return <div>child state ${state}</div>; | |
}; | |
const App = () => { | |
const [state, setState] = React.useState(); | |
return ( | |
<div className="App"> | |
<h1>Hello CodeSandbox</h1> | |
<button onClick={() => setState(Math.random(10))}>Click Me</button> | |
<div>parent state: {state}</div> | |
<Box num={state} /> | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment