Skip to content

Instantly share code, notes, and snippets.

View Tripple-A's full-sized avatar
💭
Developing software like a boss 😁

Abiodun Olowode Tripple-A

💭
Developing software like a boss 😁
View GitHub Profile
import { useReducer } from "react";
const initialState = {
age: 0,
height: 0,
hobby: "None",
};
const reducer = (state, action) => {
switch (action.type) {
<button onClick={() => dispatch({ type: "startBabyStage" })}>Baby</button>
<button onClick={() => dispatch({ type: "startTeenStage" })}>
Teenager
</button>
<button onClick={() => dispatch({ type: "startAdultStage" })}>
Adult
</button>
const [state, dispatch] = useReducer(reducer, initialState);
const { age, hobby, height } = state;
const initialState = {
age: 0,
height: 0,
hobby: "None",
};
const reducer = (state, action) => {
switch (action.type) {
case "startBabyStage":
return { ...state, age: 1, height: 29, hobby: "Biting" };
import { useState } from "react";
function App() {
const [age, setAge] = useState(0);
const [height, setHeight] = useState(0);
const [hobby, setHobby] = useState("None");
const startBabyStage = () => {
setAge(1);
setHeight(29);
@Tripple-A
Tripple-A / reducer.js
Last active July 16, 2021 13:48
A basic reducer function
const reducer = (state, action) => {
if (action.type == 'DOUBLE') {
return state * 2 ;
} else if(action.type == 'TRIPLE') {
return state * 3;
} else {
return state;
}
}