You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index/docs/01-getting-started.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,12 @@ sidebar_label: Getting started
6
6
7
7
## Preface
8
8
9
-
We know you will learn Hookstate library API very quickly without reading much (if any) of the documentation. The following will help you to get started in minutes:
9
+
We know you will learn the Hookstate library API very quickly without reading much (if any) of the documentation. The following will help you get started in minutes:
10
10
11
-
***Intuitive API**. Most of the things will be self-explanatory. More in depth description will be always there.
11
+
***Intuitive API**. Most things will be self-explanatory. More in depth description will always be there.
12
12
***Code samples** in each section are self-contained, complete and interactive. So, jump to the relevant section, copy-paste a sample to your app and extend it as needed.
13
-
***Intellisense by IDE.** If your project is in Typescript, you will benefit a lot as type inference of the API functions supports any complexity of data structures of your states. Of course, you can also use it from a plain javascript.
14
-
***Complete demo application**. There is a *Todo-list-like* complete [demo application](https://github.com/avkonst/hookstate/tree/master/docs/demos/todolist) built with Hookstate. It uses and demonstrates most of the core features of Hookstate. It gives an example how to organise your project. You may follow the example or use any other module-composition structure. Here it is running in `iframe`:
13
+
***Intellisense by IDE.** If your project is in Typescript, you will benefit a lot as type inference of the API functions supports any complexity of data structures of your states. Of course, you can also use it from plain javascript.
14
+
***Complete demo application**. There is a *Todo-list-like* complete [demo application](https://github.com/avkonst/hookstate/tree/master/docs/demos/todolist) built with Hookstate. It uses and demonstrates most of the core features of Hookstate. It gives an example of how to organise your project. You may follow the example or use any other module-composition structure. Here it is running in an`iframe`:
Hookstate consists of the core package `@hookstate/core` and optional plugins `@hookstate/*`, which you may include when needed. We have got the goal to keep the core library as small as possible but still feature-rich to address most of problems in state management and provide with good foundation for plugins. Plugins extend the library and address more specific needs. You can also write your own plugins: it is easy and gives a lot of power.
32
+
Hookstate consists of the core package `@hookstate/core` and optional plugins `@hookstate/*`, which you may include when needed. We have a goal of keeping the core library as small as possible but still feature-rich to address most of problems in state management and provide a good foundation for plugins. Plugins extend the library and address more specific needs. You can also write your own plugins: it is easy and gives a lot of power.
33
33
34
34
## Browser support
35
35
36
-
It supports all recent browsers and works where React works, including mobile applications and server side rendering.
36
+
Hookstate supports all recent browsers and works where React works, including mobile applications and server side rendering.
37
37
38
38
### IE11 support
39
39
40
-
The library supports IE11 with a few exceptions for mutations of a nested state, which have got alternative working methods.
40
+
The library supports IE11 with a few exceptions for mutations of nested state, which has alternative working methods.
41
41
The [nested state](./nested-state) section notes these exceptions.
42
42
43
-
If you need to polyfill, for example for IE11, you need to make sure the following is supported by the target environment. You may checkout existing [IE11 demo project](https://github.com/avkonst/hookstate/tree/master/docs/demos/ie11).
44
-
- ES5, `Map` and `Set` (All are available long time ago, including IE11)
43
+
If you need to polyfill, for example for IE11, you need to make sure the following is supported by the target environment. You may checkout the existing [IE11 demo project](https://github.com/avkonst/hookstate/tree/master/docs/demos/ie11).
44
+
- ES5, `Map` and `Set` (All are available a long time ago, including for IE11)
45
45
-`Symbol` (You likely already have got one from the [`react-app-polyfill`](https://www.npmjs.com/package/react-app-polyfill). If you do not import [`react-app-polyfill`](https://www.npmjs.com/package/react-app-polyfill), you can get the standalone [`es6-symbol`](https://www.npmjs.com/package/es6-symbol))
46
46
-`Number.isInteger` (Polyfill is available from [`core-js/features/number/is-integer`](https://www.npmjs.com/package/core-js))
Copy file name to clipboardExpand all lines: docs/index/docs/04-nested-state.md
+38-38Lines changed: 38 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,11 @@ sidebar_label: Nested state
6
6
7
7
import { PreviewSample } from '../src/PreviewSample'
8
8
9
-
[Local state](./local-state) and [Global state](./global-state) sections show examples, where state data is a primitive value. Next, we will have a look into one of the most powerful features of the Hookstate - the interface to a nested state of a complex object state. The interface is the same for local and global states and works equally well for both cases.
9
+
The [Local state](./local-state) and [Global state](./global-state) sections show examples where state data is a primitive value. Next, we will have a look into one of the most powerful features of Hookstate - the interface to a nested state of a complex object state. The interface is the same for local and global states and works equally well for both cases.
10
10
11
11
## Accessing and mutating nested state
12
12
13
-
Let's consider the following example where a state value is an array of objects. It demonstrates how to dive into nested state of the array and deep nested state of an element
13
+
Let's consider the following example where a state value is an array of objects. It demonstrates how to dive into the nested state of the array and deeply-nested state of an element
14
14
of the array. The state of an element is passed to a child component as a property. The child component gets and sets the deep nested state.
@@ -28,18 +28,18 @@ we append new element to the state of tasks, using `merge` method: `state.merge(
28
28
29
29
## `nested` state method
30
30
31
-
You may noticed, that a state object mixes properties from the state value object (eg. `name` property from
31
+
You may have noticed that a state object mixes properties from the state value object (eg. `name` property from
32
32
`taskState` state) and state methods (eg. `merge` property from [StateMethods](typedoc-hookstate-core#interfacesstatemethodsmd) interface). It is very likely that names of properties from your state objects will not collide with names of state methods.
33
33
In other words, your objects are unlikely to have properties named, like `merge`, `set`, `get`, etc.
34
34
Although, it is more likely your objects may have properties with names like `value` or `error`, which also
35
35
exist in state methods.
36
36
37
37
If the collision happens for whatever property name, properties
38
38
from state methods take priority and "hide" access to nested states via `.` syntax. However,
39
-
there is a `nested` state method, which allows to access nested state by name. For example
39
+
there is a `nested` state method, which allows accessing nested state by name. For example
40
40
`taskState.nested("name")` would give access to the same nested state as `taskState.name` in the example above.
41
-
So, if it ever happens
42
-
your `state` object has got a property with name, for example `set`, which collides with the corresponding
41
+
So, if it ever happens that
42
+
your `state` object has got a property with the name, for example `set`, which collides with the corresponding
43
43
state method, you would be able to obtain the nested state behind the `set` property using nested method: `state.nested("set")`.
44
44
45
45
It is also necessary to use `nested` method instead of property access by index syntax,
@@ -66,7 +66,7 @@ as the following would not be allowed by the typescript compiler:
66
66
state[props.stateKey].value
67
67
```
68
68
69
-
However, if your dynamic property name is from a set of names, which are known at compile time
69
+
However, if your dynamic property name is from a set of names which are known at compile time
70
70
and do not collide with state methods, you would be able to use property access by index syntax. For example:
71
71
72
72
```tsx
@@ -88,19 +88,19 @@ Below, you will find more about available methods for managing nested states.
88
88
89
89
### Setting new state value
90
90
91
-
Let's consider we have got the following state:
91
+
Let's consider the following state:
92
92
93
93
```tsx
94
94
const state =useState({ a: 1, b: 2 })
95
95
```
96
96
97
-
One of the state methods is `set`, which is used to set new state value.
97
+
One of the state methods is `set`, which is used to set the new state value.
98
98
99
99
```tsx
100
100
state.set({ a: 2, b: 3 })
101
101
```
102
102
103
-
New state value can be also a function, which returns new value taking the previous one:
103
+
New state value can be also a function, which returns new value and accepts the previous one:
104
104
105
105
```tsx
106
106
state.set(p=> { a: p.a+1, b: p.b-1 })
@@ -110,7 +110,7 @@ Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) in the API re
110
110
111
111
### Getting names of existing properties
112
112
113
-
Let's consider we have got the following state:
113
+
Let's consider the following state:
114
114
115
115
```tsx
116
116
const state =useState({ a: 1, b: 2 })
@@ -132,41 +132,41 @@ For a given state:
132
132
const state =useState({ a: 1, b: 2 })
133
133
```
134
134
135
-
The most efficient and recommended methods to update nested property are the following:
135
+
The most efficient and recommended methods to update a nested property are the following:
136
136
```tsx
137
137
state.a.set(p=>p+1) // increments value of property a
138
138
// or
139
139
state['a'].set(p=>p+1)
140
140
// or
141
141
state.merge(p=> ({ a: p.a+1 }))
142
142
```
143
-
It sets only property `a`, so it will rerender every component where property `a` is used.
143
+
These set only property `a`, so it will rerender every component where property `a` is used.
144
144
145
145
#### Avoid the following:
146
146
147
-
There are alternative less efficient methods, but resulting in the same mutation and data state. The following sets the entire object state to the new value (although, only `a` property is changed), so it will rerender every component where **any** property of the state is used.
147
+
There are alternative, less efficient methods, resulting in the same mutation and data state. The following sets the entire object state to the new value (although only `a` property is changed), so it will rerender every component where **any** property of the state is used.
148
148
149
149
```tsx
150
150
state.set(p=> ({ ...p, a: p.a+1 }))
151
151
```
152
152
153
-
The following sets only property `a` but uses the current property value via the [StateMethods.value](typedoc-hookstate-core.md#readonly-value), which marks the property `a` as used by a component even if it was not used during the last rendering. In other words using nested property state in rendering or in action dispatch
154
-
has got the same effect: a component is rerendered on property update.
153
+
The following sets only the property `a` but uses the current property value via the [StateMethods.value](typedoc-hookstate-core.md#readonly-value), which marks the property `a` as used by a component even if it was not used during the last rendering. In other words using nested property state in rendering or in action dispatch
154
+
has the same effect: a component is rerendered on property update.
155
155
```tsx
156
156
state['a'].set(state.a.value+1) // increments value of property a
157
157
```
158
158
159
159
Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMethods.merge](typedoc-hookstate-core.md#merge) in the API reference.
160
160
161
-
### Adding new property
161
+
### Adding a new property
162
162
163
163
For a given state:
164
164
165
165
```tsx
166
166
const state =useState<{ a:number, b?:number }>({ a: 1 }) // notice b property is optional
167
167
```
168
168
169
-
The recommended methods to add new nested property are the following:
169
+
The recommended methods to add a new nested property are the following:
170
170
```tsx
171
171
state.b.set(2)
172
172
// or
@@ -178,10 +178,10 @@ state.merge({ b: 2 })
178
178
Notice the `state` object has got **any** property defined (it is not the case in IE11),
179
179
although not every property might pass Typescript compiler check.
180
180
We accessed non existing property `b` and set it's state.
181
-
It represents the fact the state of `undefined` property is actually defined state object,
181
+
It represents the fact the state of `undefined` property is actually a defined state object,
182
182
which can be used to set `undefined` property to a new value.
183
183
184
-
It allows to add new properties to the state using the same method as for updating a property.
184
+
It allows to add new properties to the state using the same method as is used for updating a property.
185
185
186
186
#### Avoid the following
187
187
@@ -195,7 +195,7 @@ But this is the only available method for IE11.
195
195
196
196
Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMethods.merge](typedoc-hookstate-core.md#merge) in the API reference.
197
197
198
-
### Deleting existing property
198
+
### Deleting an existing property
199
199
200
200
For a given state:
201
201
@@ -257,7 +257,7 @@ Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMet
257
257
258
258
### Partial updates and deletions
259
259
260
-
You may noticed the usage of [StateMethods.merge](typedoc-hookstate-core.md#merge) above. This does partial update to the state and can insert, update and delete properties all in one call:
260
+
You may have noticed the usage of [StateMethods.merge](typedoc-hookstate-core.md#merge) above. This does a partial update to the state and can insert, update and delete properties all in one call:
261
261
262
262
```tsx
263
263
const state =useState<Record<string, number>>({
@@ -275,9 +275,9 @@ Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMet
275
275
276
276
## Advanced mutations for an array state
277
277
278
-
### Setting new state value
278
+
### Setting a new state value
279
279
280
-
Let's consider we have got the following state:
280
+
Let's consider the following state:
281
281
282
282
```tsx
283
283
const state =useState([1, 2])
@@ -299,7 +299,7 @@ Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) in the API re
299
299
300
300
### Getting indexes of existing elements
301
301
302
-
Let's consider we have got the following state:
302
+
Let's consider the following state:
303
303
304
304
```tsx
305
305
const state =useState([1, 2])
@@ -321,7 +321,7 @@ For a given state:
321
321
const state =useState([1, 2])
322
322
```
323
323
324
-
The most efficient and recommended methods to update nested element are the following:
324
+
The most efficient and recommended methods to update a nested element are the following:
325
325
```tsx
326
326
state[0].set(p=>p+1) // increments value of an element at 0 position
327
327
// or
@@ -331,7 +331,7 @@ It sets only element at `0`, so it will rerender every component where this elem
331
331
332
332
#### Avoid the following:
333
333
334
-
There are alternative less efficient methods, but resulting in the same mutation and data state. The following sets the entire array state to the new value (although, only `0` index is changed), so it will rerender every component where **any** property of the state is used.
334
+
There are alternative less efficient methods resulting in the same mutation and data state. The following sets the entire array state to the new value (although, only `0` index is changed), so it will rerender every component where **any** property of the state is used.
335
335
336
336
```tsx
337
337
state.set(p=> ([p[0] +1].concat(p.slice(1))))
@@ -346,9 +346,9 @@ state[0].set(state[0].value + 1) // increments value of an element at 0
346
346
347
347
Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMethods.merge](typedoc-hookstate-core.md#merge) in the API reference.
348
348
349
-
### Appending new element
349
+
### Appending a new element
350
350
351
-
For a given state:
351
+
To a given state:
352
352
353
353
```tsx
354
354
const state =useState([1000])
@@ -372,13 +372,13 @@ as it can be potentially less efficient than the above recommended methods:
372
372
state.set(p=>p.concat([2000]))
373
373
```
374
374
375
-
But this is the only method available for IE11 environment.
375
+
But this is the only method available for IE11.
376
376
377
377
Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMethods.merge](typedoc-hookstate-core.md#merge) in the API reference.
378
378
379
-
### Deleting existing element
379
+
### Deleting an existing element
380
380
381
-
For a given state:
381
+
From a given state:
382
382
383
383
```tsx
384
384
const state =useState([1000, 2000, 3000])
@@ -451,7 +451,7 @@ Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMet
451
451
452
452
### Splicing elements
453
453
454
-
Use `Array.prototype.splice` function to remove, replace or delete elements. In particular, this can be used to prepend new elements to an array value:
454
+
Use the `Array.prototype.splice` function to remove, replace or delete elements. In particular, this can be used to prepend new elements to an array value:
455
455
456
456
```tsx
457
457
const state =useState([3000, 4000])
@@ -463,7 +463,7 @@ state.set(p => {
463
463
464
464
### Partial updates and deletions
465
465
466
-
You may noticed the usage of [StateMethods.merge](typedoc-hookstate-core.md#merge) above. This does partial update to the state and can insert, update and delete array elements all in one call:
466
+
You may have noticed the usage of [StateMethods.merge](typedoc-hookstate-core.md#merge) above. This does a partial update to the state and can insert, update and delete array elements all in one call:
467
467
468
468
```tsx
469
469
const state =useStateLink([1000, 2000, 3000])
@@ -497,7 +497,7 @@ Learn more about [StateMethods.set](typedoc-hookstate-core.md#set) and [StateMet
497
497
498
498
## Limitations for state values
499
499
500
-
There are few limitations for state values, which are typical for any state management library for Javascript environment.
500
+
There are a few limitations for state values, which are typical for any state management library for Javascript environment.
501
501
502
-
*State value should be a JS primitive, like string/number/etc., or `null`/`undefined` or a JS object/array containing other JS primitive properties, objects or arrays. In other words, state values with Maps, Sets, Dates will not work properly.
503
-
* Cyclic and cross-referrences with the state value will not work properly.
502
+
*A state value should be a JS primitive, like string/number/etc., or `null`/`undefined` or a JS object/array containing other JS primitive properties, objects or arrays. In other words, state values with Maps, Sets, Dates will not work properly.
503
+
* Cyclic and cross-references with the state value will not work properly.
0 commit comments