@@ -4,82 +4,41 @@ import cloneDeep from "lodash.clonedeep";
44import { useLog } from "../../commons/ExampleBloc" ;
55
66const SubComponent = ( {
7- value,
8- setValue,
9- otherValue,
10- setOtherValue
7+ parentValue,
8+ setParentValue,
119} : {
12- value : number ;
13- setValue : ( newValue : number ) => void ;
14- otherValue : { str : string } ;
15- setOtherValue : ( newValue : { str : string } ) => void ;
10+ parentValue : { str : string } ;
11+ setParentValue : ( newValue : { str : string } ) => void ;
1612} ) => {
1713 const log = useLog ( ) ;
18- const [ state , setState ] = React . useState < number > ( value ) ;
19- const [ otherState , setOtherState ] = React . useState < any > ( otherValue ) ;
14+ const [ state , setState ] = React . useState < any > ( parentValue ) ;
2015 const [ shouldSetSuperState , setShouldSetSuperState ] = React . useState < boolean > ( false ) ;
2116
2217 log ( "render" ) ;
2318
24- React . useEffect ( ( ) => {
25- setState ( value ) ;
26- } , [ value , setState ] ) ;
2719 React . useEffect ( ( ) => {
2820 log ( "use effect" ) ;
29- if ( shouldSetSuperState ) setOtherValue ( otherValue ) ;
30- else setOtherState ( otherValue ) ;
31- } , [ otherValue , setOtherState , setOtherValue , shouldSetSuperState , log ] ) ;
21+ if ( shouldSetSuperState ) setParentValue ( state ) ;
22+ setState ( parentValue ) ;
23+ } , [ parentValue , setParentValue , state , setState , shouldSetSuperState , log ] ) ;
3224
3325 return (
3426 < >
35- < pre > { JSON . stringify ( { value , state, otherState , shouldSetSuperState } , null , 2 ) } </ pre >
27+ < pre > { JSON . stringify ( { state, shouldSetSuperState } , null , 2 ) } </ pre >
3628 < ul >
37- < li >
38- < ActionButton
39- label = "Increment state"
40- onClick = { ( ) => {
41- setState ( state => state + 1 ) ;
42- } }
43- />
44- </ li >
45- < li >
46- < ActionButton
47- label = "Increment super state"
48- onClick = { ( ) => {
49- setValue ( value + 1 ) ;
50- } }
51- />
52- </ li >
5329 < li >
5430 < ActionButton
5531 label = "Change other state"
5632 onClick = { ( ) => {
57- setOtherState ( { someProps : "zefzefz" } ) ;
58- } }
59- />
60- </ li >
61- < li >
62- < ActionButton
63- label = "Change super other state"
64- onClick = { ( ) => {
65- setOtherValue ( { str : "zezfez" } ) ;
66- } }
67- />
68- </ li >
69- < li >
70- < ActionButton
71- label = "Reset super state"
72- onClick = { ( ) => {
73- setValue ( 0 ) ;
74- setOtherValue ( { str : "" } ) ;
33+ setState ( { someProps : "zefzefz" } ) ;
7534 } }
7635 />
7736 </ li >
7837 < li >
7938 < ActionButton
8039 label = "Change state option"
8140 onClick = { ( ) => {
82- setShouldSetSuperState ( state => ! state ) ;
41+ setShouldSetSuperState ( ( state ) => ! state ) ;
8342 } }
8443 />
8544 </ li >
@@ -89,17 +48,9 @@ const SubComponent = ({
8948} ;
9049
9150const ExampleUseState106 = ( ) => {
92- const [ counter , setCounter ] = React . useState < number > ( 0 ) ;
9351 const [ str , setStr ] = React . useState < { str : string } > ( { str : "" } ) ;
9452
95- return (
96- < SubComponent
97- value = { counter }
98- setValue = { setCounter }
99- otherValue = { cloneDeep ( str ) }
100- setOtherValue = { setStr }
101- />
102- ) ;
53+ return < SubComponent parentValue = { cloneDeep ( str ) } setParentValue = { setStr } /> ;
10354} ;
10455
10556export default ExampleUseState106 ;
0 commit comments