Skip to content

Commit cb0d6f4

Browse files
committed
update documentation around setState and afterRender
1 parent 2e01132 commit cb0d6f4

2 files changed

Lines changed: 125 additions & 5 deletions

File tree

documentation/rendering.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,33 @@ While you could use a map function for loops and raw ternary operators of condit
3636
</ul>
3737
```
3838

39-
The library also includes a couple afterRender hooks.
39+
The library also includes a couple afterRender directives that can be applied to the each and when control flow.
4040

4141
### selectWhen(signal, handler)
4242
### selectEach(signal, handler)
4343

44-
These trigger on the signal to indicate the selected model/s and calls the handler function with associated element, and a boolean to indicate whether the model is selected or not.
44+
These trigger on the signal to indicate the selected model/s and calls the handler function with associated element, and a boolean to indicate whether the model is selected or not. If the handler is a string instead of a function the default behavior is to toggle a class with the string name.
45+
46+
These directives also require setting a model on the child element in order to identify the node.
47+
48+
```js
49+
const [state, setState] = createState({
50+
list: [ /* ... */ ],
51+
selected: [ /* ... */]
52+
})
53+
54+
/* .... */
55+
56+
<$
57+
each={state.list}
58+
afterRender={selectEach(
59+
() => state.selected,
60+
(node, selected) => node.toggleClass('selected', selected)
61+
)}
62+
>{ item =>
63+
<div model={item} onClick={select} />
64+
}</$>
65+
```
4566

4667
## Custom Directives
4768

documentation/state.md

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,112 @@ Initializes with object value and returns an array where the first index is the
1212
### `setState(...path, changes)`
1313
### `setState([...path, changes], [...path, changes])`
1414

15-
This merges the changes into the path on the state object. All changes in set operation are applied at the same time so it is often more optimal than replace.
15+
This merges the changes into the path on the state object. All changes made in a single setState command are applied syncronously (ie all changes see each other at the same time). Changes can take the form of function that passes previous state and returns new state or a value. Objects are always merged.
1616

17-
Alternatively if you can do multiple sets in a single call by passing an array of paths and changes.
17+
```js
18+
const [state, setState] = createState({ firstName: 'John', lastName: 'Miller' });
19+
20+
setState({ firstName: 'Johnny', middleName: 'Lee' })
21+
// ({ firstName: 'Johnny', middleName: 'Lee', lastName: 'Miller' })
22+
23+
setState(state => { preferredName: state.firstName, lastName: 'Milner' });
24+
// ({ firstName: 'Johnny', preferredName: 'Johnny', middleName: 'Lee', lastName: 'Milner' })
25+
```
26+
27+
The function form is not terribly useful top level given Solid's synchronous nature. However, setState also supports nested setting where you can indicate the path to the change. When nested the state you are updating may be other non Object values. Objects are still merged but other values (including Arrays) are replaced.
1828

29+
```js
30+
const [state, setState] = createState({
31+
counter: 2,
32+
list: [
33+
{ id: 23, title: 'Birds' }
34+
{ id: 27, title: 'Fish' }
35+
]
36+
});
37+
38+
setState('counter', c => c + 1);
39+
setState('list', l => [...l, {id: 43, title: 'Marsupials'}]);
40+
setState('list', 2, 'read', true);
41+
// {
42+
// counter: 3,
43+
// list: [
44+
// { id: 23, title: 'Birds' }
45+
// { id: 27, title: 'Fish' }
46+
// { id: 43, title: 'Marsupials', read: true }
47+
// ]
48+
// }
49+
```
1950
Path can be string keys, array of keys, wildcards ('*'), iterating objects ({from, to, by}), or filter functions. This gives incredible expressive power to describe state changes.
2051

21-
All changes made in a single setState command are applied syncronously (ie all changes see each other at the same time).
52+
```js
53+
const [state, setState] = createState({
54+
todos: [
55+
{ task: 'Finish work', completed: false }
56+
{ task: 'Go grocery shopping', completed: false }
57+
{ task: 'Make dinner', completed: false }
58+
]
59+
});
60+
61+
setState('todos', [0, 2], 'completed', true);
62+
// {
63+
// todos: [
64+
// { task: 'Finish work', completed: true }
65+
// { task: 'Go grocery shopping', completed: false }
66+
// { task: 'Make dinner', completed: true }
67+
// ]
68+
// }
69+
70+
setState('todos', { from: 0, to: 1 }, 'completed', c => !c);
71+
// {
72+
// todos: [
73+
// { task: 'Finish work', completed: false }
74+
// { task: 'Go grocery shopping', completed: true }
75+
// { task: 'Make dinner', completed: true }
76+
// ]
77+
// }
78+
79+
setState('todos', todo => todo.completed, 'title', t => t + '!')
80+
// {
81+
// todos: [
82+
// { task: 'Finish work', completed: false }
83+
// { task: 'Go grocery shopping!', completed: true }
84+
// { task: 'Make dinner!', completed: true }
85+
// ]
86+
// }
87+
88+
setState('todos', '*', todo => { marked: true, completed: !todo.completed })
89+
// {
90+
// todos: [
91+
// { task: 'Finish work', completed: true, marked: true }
92+
// { task: 'Go grocery shopping!', completed: false, marked: true }
93+
// { task: 'Make dinner!', completed: false, marked: true }
94+
// ]
95+
// }
96+
```
97+
98+
Additionally you can do multiple sets in a single call by passing an array of paths and changes.
99+
100+
```js
101+
const [state, setState] = createState({
102+
counter: 2,
103+
list: [
104+
{ id: 23, title: 'Birds' }
105+
{ id: 27, title: 'Fish' }
106+
]
107+
});
108+
109+
setState(
110+
['counter', c => c * 3],
111+
['list', 1, 'title', t => t + '!']
112+
);
113+
// {
114+
// counter: 6,
115+
// list: [
116+
// { id: 23, title: 'Birds' }
117+
// { id: 27, title: 'Fish!' }
118+
// ]
119+
// }
120+
```
22121

23122
### `reconcile(...path, value)`
24123

0 commit comments

Comments
 (0)