"react reducer hooks" Code Answer's
You're definitely familiar with the best coding language Javascript that developers use to develop their projects and they get all their queries like "react reducer hooks" answered properly. Developers are finding an appropriate answer about react reducer hooks related to the Javascript coding language. By visiting this online portal developers get answers concerning Javascript codes question like react reducer hooks. Enter your desired code related query in the search bar and get every piece of information about Javascript code related question on react reducer hooks.
useReducer
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>
</>
);
}
Source: reactjs.org
useReducer
function init(initialCount) { return {count: initialCount};}
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
case 'reset': return init(action.payload); default:
throw new Error();
}
}
function Counter({initialCount}) {
const [state, dispatch] = useReducer(reducer, initialCount, init); return (
<>
Count: {state.count}
<button
onClick={() => dispatch({type: 'reset', payload: initialCount})}> Reset
</button>
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
Source: reactjs.org
react reducer hooks
import React from "react";
// initial state
const initialState = {counter: 0};
// action type
const INC = 'INCREMENT';
const DEC = 'DECREMENT';
// action creator inc
function incActionCreator() {
return {
type: INC,
}
}
// action creator dec
function decActionCreator() {
return {
type: DEC,
}
}
// action reducer
function hooksReducer(state, action) {
switch(action.type) {
case 'INCREMENT':
return {
...state,
counter: state.counter++
}
case 'DECREMENT':
return {
...state,
counter: state.counter--
}
default:
return state;
}
}
// app component
function App () {
// useReducer is very similar to the store in Redux
const [stateAction, setDispatchAction] = React.useReducer(hooksReducer, initialState);
const incClick = (e) => setDispatchAction(incActionCreator())
const decClick = (e) => setDispatchAction(decActionCreator())
return (
<>
<button onClick={incClick}>Incerement</button>
<button onClick={decClick}>Decrement</button>
<h4>Counter: {stateAction.counter}</h4>
</>
);
}
export default App;
All those coders who are working on the Javascript based application and are stuck on react reducer hooks can get a collection of related answers to their query. Programmers need to enter their query on react reducer hooks related to Javascript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about react reducer hooks for the programmers working on Javascript code while coding their module. Coders are also allowed to rectify already present answers of react reducer hooks while working on the Javascript language code. Developers can add up suggestions if they deem fit any other answer relating to "react reducer hooks". Visit this developer's friendly online web community, CodeProZone, and get your queries like react reducer hooks resolved professionally and stay updated to the latest Javascript updates.