@@ -6,75 +6,87 @@ import React, {
66 useEffect ,
77} from 'react' ;
88
9- import useContextDevTools from '../../index' ;
9+ import useContextDevTools from '../../index.min ' ;
1010
1111export type Todo = {
1212 id : number ;
1313 text : string ;
1414 done : boolean ;
1515} ;
1616
17- export type TodosState = Todo [ ] ;
17+ export type TodosState = { root : Todo [ ] } ;
1818
1919const TodosStateContext = createContext < TodosState | undefined > ( undefined ) ;
2020
2121type Action =
2222 | { type : 'CREATE' ; text : string }
2323 | { type : 'TOGGLE' ; id : number }
24- | { type : 'REMOVE' ; id : number } ;
24+ | { type : 'REMOVE' ; id : number }
25+ | { type : 'IMPORT_STATE' ; state : any } ;
2526
2627type TodosDispatch = Dispatch < Action > ;
2728const TodosDispatchContext = createContext < TodosDispatch | undefined > (
2829 undefined
2930) ;
3031
31- function todosReducer ( state : TodosState , action : Action ) : TodosState {
32+ function todosReducer ( givenState : TodosState , action : Action ) : TodosState {
33+ const state = givenState . root ;
3234 switch ( action . type ) {
33- case 'CREATE' :
34- const nextId = Math . max ( ...state . map ( todo => todo . id ) ) + 1 ;
35- return state . concat ( {
36- id : nextId ,
37- text : action . text ,
38- done : false
39- } ) ;
40- case 'TOGGLE' :
41- return state . map ( todo =>
42- todo . id === action . id ? { ...todo , done : ! todo . done } : todo
43- ) ;
44- case 'REMOVE' :
45- return state . filter ( todo => todo . id !== action . id ) ;
46- default :
47- throw new Error ( 'Unhandled action' ) ;
48- }
35+ case 'CREATE' :
36+ const nextId = Math . max ( ...state . map ( ( todo ) => todo . id ) ) + 1 ;
37+ return {
38+ root : state . concat ( {
39+ id : ( Number . isFinite ( nextId ) && nextId ) || 1 ,
40+ text : action . text ,
41+ done : false ,
42+ } ) ,
43+ } ;
44+ case 'TOGGLE' :
45+ return {
46+ root : state . map ( ( todo ) =>
47+ todo . id === action . id ? { ...todo , done : ! todo . done } : todo
48+ )
49+ }
50+ case 'REMOVE' :
51+ return {
52+ root : state . filter ( ( todo ) => todo . id !== action . id ) ,
53+ } ;
54+ case 'IMPORT_STATE' :
55+ return action . state ;
56+ default :
57+ throw new Error ( 'Unhandled action' ) ;
58+ }
4959}
5060
5161export function TodosContextProvider ( {
5262 children
5363} : {
5464 children : React . ReactNode ;
5565} ) {
56- const [ todos , dispatch ] = useReducer ( todosReducer , [
57- {
58- id : 1 ,
59- text : 'Learn Context API' ,
60- done : true ,
61- } ,
62- {
63- id : 2 ,
64- text : 'Learn TypeScript' ,
65- done : true ,
66- } ,
67- {
68- id : 3 ,
69- text : 'Use Context API with TypeScript' ,
70- done : false ,
71- } ,
72- ] ) ;
66+ const [ todos , dispatch ] = useReducer ( todosReducer , {
67+ root : [
68+ {
69+ id : 1 ,
70+ text : 'My first todo using context API' ,
71+ done : true ,
72+ } ,
73+ {
74+ id : 2 ,
75+ text : 'Second one to try time travel debugging' ,
76+ done : true ,
77+ } ,
78+ {
79+ id : 3 ,
80+ text : '3rd one to import new todos' ,
81+ done : false ,
82+ } ,
83+ ] ,
84+ } ) ;
7385
7486 const devTools = useContextDevTools ( dispatch ) ;
7587
7688 useEffect ( ( ) => {
77- devTools . sendUpdatedState ( todos ) ;
89+ devTools . sendUpdatedState ( todos ) ;
7890 } , [ todos , devTools ] ) ;
7991
8092
0 commit comments