File tree 1 file changed +20
-3
lines changed
ReactByPrasadSir/src/components/11_useReducer/examples
1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react'
1
+ import React , { useReducer } from 'react'
2
2
3
3
const UseReducerEx2 = ( ) => {
4
+ const initialState = 0
5
+ let reducer = ( state , action ) => {
6
+ switch ( action . type ) {
7
+ case 'add' :
8
+ return state + action . value ;
9
+
10
+ case 'sub' :
11
+ return state - action . value ;
12
+
13
+ default :
14
+ return state ;
15
+ }
16
+
17
+ }
18
+ const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
19
+
4
20
return (
5
21
< div >
6
- < h1 className = 'p-2 text-xl' > UseReducer Ex1</ h1 >
7
- { /* photo clicked */ }
22
+ < h1 className = 'p-2 text-xl' > UseReducer Ex2 using objects to pass values</ h1 >
23
+ < button onClick = { ( ) => { dispatch ( { type :'add' , value : 10 } ) } } className = 'p-2 rounded-md bg-orange-500 text-white' > Add: { state } </ button >
24
+ < button onClick = { ( ) => { dispatch ( { type :'sub' , value : 2 } ) } } className = 'p-2 mx-2 rounded-md bg-orange-500 text-white' > Sub: { state } </ button >
8
25
</ div >
9
26
)
10
27
}
You can’t perform that action at this time.
0 commit comments