xxxxxxxxxx
const [state, dispatch] = useReducer(reducer, initialState);
xxxxxxxxxx
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
useReducer is a hook in React that allows you to manage state in your components. It is similar to useState, but it is designed for managing state that is more complex or requires the ability to perform more than just simple state updates.
Here is an example of how useReducer might be used:
xxxxxxxxxx
const [state, dispatch] = useReducer(reducer, initialState);
//In this example, useReducer takes two arguments: a "reducer"
//function and an initial state. The reducer is a function that takes
//in the current state and an action, and returns a new state.
//The initial state is the state that the component will start with.
//The useReducer hook returns an array with two elements:
//the current state and a dispatch function. The dispatch function is
//used to send actions to the reducer, which will then update the state.
//Here is an example of how the reducer function might look:
function reducer(state, action) {
switch (action.type) {
case 'INCREMENT':
return {count: state.count + 1};
case 'DECREMENT':
return {count: state.count - 1};
default:
return state;
}
}
// In this example, the reducer function is handling two actions:
// INCREMENT and DECREMENT. When the INCREMENT action is dispatched,
//the reducer will return a new state with the count field incremented by 1.
//When the DECREMENT action is dispatched, the reducer will return a new state with the count field decremented by 1.
// To continue using the useReducer hook in your component, you can call
//the dispatch function with an action object as an argument. For example:
dispatch({type: 'INCREMENT'});
//This will cause the reducer function to be called with the INCREMENT action,
//which will update the state and cause the component to re-render.
//You can also use the current state and the dispatch function in your
//component's JSX:
<button onClick={() => dispatch({type: 'INCREMENT'})}>Increment</button>
<p>Count: {state.count}</p>
// In this example, clicking the "Increment" button will dispatch the INCREMENT action,
//which will update the state and cause the component to re-render with the updated count.
xxxxxxxxxx
import './App.css';
import { useState, useReducer } from 'react';
function reducer(state, action) {
switch (action.type) {
case 'increment':
return ({ state, count: state.count + 1 })
case 'decrement':
return ({ state, count: state.count - 1 })
case 'tgColor':
return ({ state, color: (!state.color) })
case 'handleInputChange':
return ({ state, name: action.payload })
}
}
function App() {
const [state, dispatch] = useReducer(reducer, { count: 0, color: false, name: "" })
// const [name, setName] = useState("")
// const [count, setCount] = useState(0)
// const [color, setColor] = useState(false);
const handleInputChange = (e) => {
dispatch({ type: "handleInputChange", payload: e.target.value })
}
const colorCode = "#b81f00";
return (
<div className="App">
<form action="">
<input type="text" onChange={handleInputChange} value={state.name} placeholder="Enter Name" />
</form>
<h3 style={state.color ? { color: "red" } : { color: "black" }}>{state.count}</h3>
<button className="btn" onClick={() => dispatch({ type: "increment" })}>+</button>
<button className="btn" onClick={() => dispatch({ type: "decrement" })}>-</button>
<button className="btn" onClick={() => dispatch({ type: "tgColor" })}>Color</button>
<h2 style={state.color ? { color: "red" } : { color: "black" }}>{state.name}</h2>
</div>
);
}
export default App;
xxxxxxxxxx
import React, { useReducer } from "react";
const reducer = (state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
break;
case "decrement":
return { count: state.count - 1 };
break;
default:
throw new Error();
}
};
const UseReduceExample = () => {
const [state, dispatch] = useReducer(reducer, initialCount );
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "increment" })}>+</button>
</>
);
};
export default UseReduceExample;
xxxxxxxxxx
import React from "react";
import { useReducer } from "react";
const initialState = {
count: 0,
};
type ReducerProps = {
state: any;
};
const reducer = (state: state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + action.payload };
case "decrement":
return { count: state.count - action.payload };
default:
return state;
}
};
const Counter = () => {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
Count : {state.count}
<button
onClick={() => {
dispatch({ type: "increment", payload: 1 });
}}
>
Increment ++
</button>
<button
onClick={() => {
dispatch({ type: "decrement", payload: 1 });
}}
>
Decrement --
</button>
</div>
);
};
export default Counter;
xxxxxxxxxx
import React, { useReducer } from "react";
const reducer = (state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
break;
case "decrement":
return { count: state.count - 1 };
break;
default:
throw new Error();
}
};
const UseReduceExample = () => {
const [state, dispatch] = useReducer(reducer, initialCount );
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "increment" })}>+</button>
</>
);
};
export default UseReduceExample;
xxxxxxxxxx
import React, { useReducer } from "react";
const reducer = (state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
break;
case "decrement":
return { count: state.count - 1 };
break;
default:
throw new Error();
}
};
const UseReduceExample = () => {
const [state, dispatch] = useReducer(reducer, initialCount );
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "increment" })}>+</button>
</>
);
};
export default UseReduceExample;
xxxxxxxxxx
import React, { useReducer } from 'react';
const reducer = (state, action) => {
// Logic to update the state based on the action type and payload
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
// Handle other action types here
default:
return state;
}
};
const initialState = { count: 0 };
const Counter = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const increment = () => {
dispatch({ type: 'INCREMENT' });
};
const decrement = () => {
dispatch({ type: 'DECREMENT' });
};
return (
<div>
<p>Count: {state.count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
};
xxxxxxxxxx
import React, { useReducer } from "react";
const reducer = (state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
break;
case "decrement":
return { count: state.count - 1 };
break;
default:
throw new Error();
}
};
const UseReduceExample = () => {
const [state, dispatch] = useReducer(reducer, initialCount );
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "increment" })}>+</button>
</>
);
};
export default UseReduceExample;
xxxxxxxxxx
import React, { useReducer } from "react";
const reducer = (state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
break;
case "decrement":
return { count: state.count - 1 };
break;
default:
throw new Error();
}
};
const UseReduceExample = () => {
const [state, dispatch] = useReducer(reducer, initialCount );
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "increment" })}>+</button>
</>
);
};
export default UseReduceExample;