userreducer1
https://react.dev/reference/react/useReducer
....use cases:---
1:-use full when u have closely related piece of state
2:usefull when future state value is depends on current state
import React,{useReducer} from 'react'
function reducer(state=count, action) {
if (action.type ==='increment' ) {
return {
...state//future point to hold state values
count:state.count+1
};
}
if (action.type ==='decrement' ) {
return {
count:state.count-1
};
}
if (action.type ==='multiply' ) {
return {
count:state.count*5
};
}
}
export default function Random1() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div>
{state.count}
<button onClick={()=>{dispatch({type:'increment'})}}>increment</button>
<button onClick={()=>{dispatch({type:'decrement'})}}>decrement</button>
<button onClick={()=>{dispatch({type:'multiply'})}}>multiply</button>
</div>
)
}
....use cases:---
import React, { useReducer,useState } from 'react'
export default function CounterPage() {
const INCREMENT_COUNT='increment-count';//tells the action type
const DECREMENT_COUNT='decrement-count';
const ADD_VALUE_TO_COUNT='add-value-to_count';
const reducer=(state,action)=>{
if (action.type ===INCREMENT_COUNT ) {
// console.log('hello');
return {
...state,//future point to hold state values
count:state.count+1
};
}
if (action.type ===DECREMENT_COUNT ) {
// console.log('hello');
return {
...state,//future point to hold state values
count:state.count-1
};
}
if (action.type ==='change-value-to-add' ) {
// console.log('hello');
return {
...state,//future point to hold state values
valueToAdd:action.payload//set to value
};
}
if (action.type ===ADD_VALUE_TO_COUNT ) {
// console.log('hello');
return {
...state,//future point to hold state values
count:state.count+state.valueToAdd
};
}
return state;//atlease current state..
}
const [state,dispatch]=useReducer(reducer,{
count:0,
valueToAdd:0
})
const handleChange=(e)=>{
const value= parseInt(e.target.value)||0;
// console.log(value);
dispatch({
type:'change-value-to-add',
payload:value
});
// setValueTOAdd(value);
}
return (
<div>
{state.count}
<button onClick={()=>{dispatch({type:INCREMENT_COUNT})}}>add</button>
<button onClick={()=>{dispatch({type:DECREMENT_COUNT})}}>sub</button>
<input type="text" value={state.valueToAdd||''}
onChange={handleChange}/>
<button onClick={()=>{dispatch({type:ADD_VALUE_TO_COUNT})}}>add</button>
</div>
)
}
1:--
import React, { useReducer,useState } from 'react'
export default function CounterPage() {
const INCREMENT_COUNT='increment-count';//tells the action type
const DECREMENT_COUNT='decrement-count';
const ADD_VALUE_TO_COUNT='add-value-to_count';
const reducer=(state,action)=>{
if (action.type ===INCREMENT_COUNT ) {
// console.log('hello');
return {
...state,//future point to hold state values
count:state.count+1
};
}
if (action.type ===DECREMENT_COUNT ) {
// console.log('hello');
return {
...state,//future point to hold state values
count:state.count-1
};
}
if (action.type ==='change-value-to-add' ) {
// console.log('hello');
return {
...state,//future point to hold state values
valueToAdd:action.payload//set to value
};
}
if (action.type ==='add_value_S' ) {
// console.log('hello');
return {
...state,//future point to hold state values
selectValue:action.payload,//state change
//set to value
count:state.count+action.payload
};
}
if (action.type ===ADD_VALUE_TO_COUNT ) {
// console.log('hello');
return {
...state,//future point to hold state values
count:state.count+state.valueToAdd
};
}
return state;//atlease current state..
}
const [state,dispatch]=useReducer(reducer,{
count:0,
valueToAdd:0,
selectValue:0
})
const handleChange=(e)=>{
const value= parseInt(e.target.value)||0;
// console.log(value);
dispatch({
type:'change-value-to-add',
payload:value
});
}
const handleSelect=(e)=>{
const svalue= parseInt(e.target.value)||10;
// console.log(value);
dispatch({
type:'add_value_S',
payload:svalue
});
}
// setValueTOAdd(value);
return (
<div>
{state.count}
<button onClick={()=>{dispatch({type:INCREMENT_COUNT})}}>add</button>
<button onClick={()=>{dispatch({type:DECREMENT_COUNT})}}>sub</button>
<input type="text" value={state.valueToAdd||''}
onChange={handleChange}/>
<button onClick={()=>{dispatch({type:ADD_VALUE_TO_COUNT})}}>add</button>
<select value={state.selectValue||''} onChange={handleSelect}>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
</select>
</div>
)
}
2:-- u can change the code in switch case
Comments
Post a Comment