Skip to content

Commit

Permalink
refactoring useState to useReducer
Browse files Browse the repository at this point in the history
  • Loading branch information
jessilyneh committed Mar 1, 2022
1 parent edc5982 commit c2bb644
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import ReactDOM from "react-dom";
import "./index.css";

export default function App() {
//first argument: what we want to do if setNumber has called?
// 0: the initial state
const [number, setNumber] = useReducer(
(number, newNumber) => number + newNumber,
0
);
const [checked, toggle] = useReducer(
(checked) => !checked, false);

return <h1 onClick={()=> setNumber(1)}>{number}</h1>;
return (
<div>
<input
type="checkbox"
value={checked}
onChange={toggle}
/>
<p>{checked ? "checked" : "no checked"}</p>
</div>
)
}


ReactDOM.render(
<React.StrictMode>
<App />
Expand Down

0 comments on commit c2bb644

Please sign in to comment.