Skip to content

Commit 58be7a1

Browse files
Merge pull request #3 from Minky1703/main
useReducer n useState hook
2 parents 356b5d2 + 1ed08f4 commit 58be7a1

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Child1 } from './components/Childs/Child1';
66
import { Click } from './components/Events/click';
77
import { Input } from './components/Events/Input';
88
import { Login } from './components/Hooks/Login';
9+
import { User } from './components/Hooks/User';
10+
import {Counter} from './components/Hooks/Counter'
911
const App =() => {
1012
return (
1113
<>
@@ -21,6 +23,10 @@ const App =() => {
2123
<Input value='' handleChange={(event)=> console.log('Input event',event) } />
2224
<h1>HOOKS</h1>
2325
<Login />
26+
<h2>User : usestate adv</h2>
27+
<User />
28+
<h1>Reducer Hook</h1>
29+
<Counter />
2430
</>
2531
);
2632
}

src/components/Hooks/Counter.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {useReducer} from 'react'
2+
3+
type CounterState={
4+
count: number
5+
}
6+
type CounterAction={
7+
type: string
8+
payload: number
9+
}
10+
const initialState = {
11+
count : 0
12+
}
13+
14+
function reducer(state:CounterState,action:CounterAction){
15+
switch(action.type){
16+
case 'increment':
17+
return {count: state.count + action.payload}
18+
case 'decrement':
19+
return {count: state.count - action.payload}
20+
default:
21+
return state;
22+
}
23+
}
24+
export const Counter = () => {
25+
const [state,dispatch] = useReducer(reducer,initialState);
26+
27+
return (
28+
<>
29+
<h1>Counter</h1>
30+
Count : {state.count}
31+
<button onClick={() => dispatch({type: 'increment', payload: 10})}>
32+
INC
33+
</button>
34+
<button onClick={() => dispatch({type: 'decrement', payload: 10})}>
35+
DEC
36+
</button>
37+
</>
38+
)
39+
}

src/components/Hooks/User.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {useState} from 'react'
2+
3+
type AuthUser = {
4+
name : string,
5+
email : string,
6+
}
7+
type dummyUser = {
8+
name : string,
9+
email : string,
10+
}
11+
export const User = () => {
12+
const [user, setUser] = useState<AuthUser | null>(null);
13+
const [test,setTest] = useState<dummyUser>({} as dummyUser);
14+
15+
const logout = () => {
16+
setUser(null);
17+
setTest({} as dummyUser);
18+
}
19+
const login = () => {
20+
setUser({
21+
name: 'John Doe',
22+
email: 'Joe@something.com'
23+
})
24+
25+
setTest({
26+
name: 'John Doe',
27+
email: 'sdfsd'
28+
})
29+
}
30+
return (
31+
<>
32+
<div>User</div>
33+
<button onClick={login}>Login</button>
34+
<div>{user?.email} : {user?.name}</div>
35+
<p> {test.email} : {test.name} </p>
36+
<button onClick={logout} >Logout</button>
37+
</>
38+
)
39+
}

0 commit comments

Comments
 (0)