-
Notifications
You must be signed in to change notification settings - Fork 106
/
plugin-initial-statefragment.tsx
37 lines (36 loc) · 1.46 KB
/
plugin-initial-statefragment.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React from 'react';
import { useStateLink, StateMemo, StateFragment } from '@hookstate/core';
import { Initial } from '@hookstate/initial';
export const ExampleComponent = () => {
const state = useStateLink(['First Task', 'Second Task'])
.with(Initial); // enable the plugin
return <>
<StateFragment
state={state}
transform={StateMemo((s) => Initial(s).modified())}
>{(modified: boolean) => {
return <p>
Last render at: {(new Date()).toISOString()} <br/>
Is whole current state modified (vs the initial): {modified.toString()} <br/>
The <b>initial</b> state: {JSON.stringify(Initial(state).get())}
</p>
}}
</StateFragment>
{state.nested.map((taskState, taskIndex) =>
<StateFragment key={taskIndex} state={taskState}>{scopedTaskState => {
return <p>
Last render at: {(new Date()).toISOString()} <br/>
Is this task modified: {Initial(scopedTaskState).modified().toString()} <br/>
<input
value={scopedTaskState.get()}
onChange={e => scopedTaskState.set(e.target.value)}
/>
</p>
}}
</StateFragment>
)}
<p><button onClick={() => state.set(tasks => tasks.concat(['New Task']))}>
Add task
</button></p>
</>
}