Skip to content

Instantly share code, notes, and snippets.

@allenwei
Last active March 17, 2020 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenwei/ab78ea149de2fc18af90a1d10aa3ed05 to your computer and use it in GitHub Desktop.
Save allenwei/ab78ea149de2fc18af90a1d10aa3ed05 to your computer and use it in GitHub Desktop.
useState with props Wrong way
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