File tree Expand file tree Collapse file tree 5 files changed +104
-3
lines changed Expand file tree Collapse file tree 5 files changed +104
-3
lines changed Original file line number Diff line number Diff line change 66 {
77 "name" : " redux toolkit" ,
88 "link" : " /redux"
9+ },
10+ {
11+ "name" : " useState" ,
12+ "link" : " /usestate"
913 }
1014]
Original file line number Diff line number Diff line change 1+ import { useState } from 'react'
2+ import Card from '@/common/ui/card'
3+ import Page from '@/common/layout/page'
4+ import TodoListComponentV3 from '@/domain/usestate/todolist'
5+
6+ function UseStateComponent ( ) {
7+ const [ state , setState ] = useState ( [ ] )
8+
9+ const addTodo = ( ) => {
10+ const data = [ ...state ]
11+ data . push ( {
12+ id : Math . random ( ) . toString ( 36 ) . substring ( 2 , 8 ) ,
13+ text : 'Hello, wooorld!!'
14+ } )
15+
16+ setState ( data )
17+ }
18+
19+ const deleteTodo = ( id ) => {
20+ const temp = state . filter ( item => item . id !== id )
21+ setState ( temp )
22+ }
23+
24+ return (
25+ < Page >
26+ < h2 >
27+ useState
28+ </ h2 >
29+
30+ < p > Testing page re-renders and local state set by useState rendering from inside a deeply-nested component.</ p >
31+ < br />
32+
33+ < button onClick = { addTodo } >
34+ Create Todo
35+ </ button >
36+ < br /> < br />
37+
38+ < Card >
39+ < Card >
40+ < Card >
41+ < Card >
42+ < Card >
43+ < Card >
44+ < TodoListComponentV3
45+ todos = { state }
46+ addTodo = { addTodo }
47+ deleteTodo = { deleteTodo }
48+ />
49+ </ Card >
50+ </ Card >
51+ </ Card >
52+ </ Card >
53+ </ Card >
54+ </ Card >
55+ </ Page >
56+ )
57+ }
58+
59+ export default UseStateComponent
Original file line number Diff line number Diff line change 1+ import PropTypes from 'prop-types'
2+
3+ function TodoListComponentV3 ( {
4+ todos,
5+ deleteTodo
6+ } ) {
7+ return (
8+ < ul style = { { marginTop : '24px' } } >
9+ { ( todos ) . map ( ( ( item , index ) => (
10+ < li key = { index } >
11+ < span > id: { item . id } , { item . text } </ span >
12+ < span >
13+ < button onClick = { ( ) => deleteTodo ( item . id ) } >
14+ [ x ]
15+ </ button >
16+ </ span >
17+ </ li >
18+ ) ) ) }
19+ </ ul >
20+ )
21+ }
22+
23+ TodoListComponentV3 . propTypes = {
24+ deleteTodo : PropTypes . func
25+ }
26+
27+ export default TodoListComponentV3
Original file line number Diff line number Diff line change @@ -4,7 +4,9 @@ import { store } from '@/store/store'
44import '@/styles/globals.css'
55
66export default function App ( { Component, pageProps } ) {
7- return < Provider store = { store } >
8- < Component { ...pageProps } />
9- </ Provider >
7+ return (
8+ < Provider store = { store } >
9+ < Component { ...pageProps } />
10+ </ Provider >
11+ )
1012}
Original file line number Diff line number Diff line change 1+ import UseStateComponent from '@/components/usestate'
2+
3+ function UseStateContainer ( ) {
4+ return (
5+ < UseStateComponent />
6+ )
7+ }
8+
9+ export default UseStateContainer
You can’t perform that action at this time.
0 commit comments