chaesthetics / FrontEnd-Dev

For Meta Certification
0 stars 0 forks source link

useReducer #23

Open chaesthetics opened 1 year ago

chaesthetics commented 1 year ago
import { useReducer } from "react";

const reducer = (state, action) => {
  if(action.type === "buy") return {money: state.money - 10}
  if(action.type === "sell") return {money: state.money + 10}
  return state;
}

function App(){

  const initialState = {money: 100};
  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <div className="App">
      <header className="App-header">
        <>
          <h1>Wallet:{state.money}</h1>
          <button onClick={()=>dispatch({type: "buy"})}>Buy Meal</button>
          <button onClick={()=>dispatch({type: "sell"})}>Serve Meal</button>
        </>
      </header>
    </div>
  );
}

export default App;