@@ -32,7 +32,7 @@ use_state
32
32
Returns a stateful value and a function to update it.
33
33
34
34
During the first render the ``state `` will be identical to the ``initial_state `` passed
35
- as the first argument. However in subsiquent renders ``state `` will take on the value
35
+ as the first argument. However in subsequent renders ``state `` will take on the value
36
36
passed to ``set_state ``.
37
37
38
38
.. code-block ::
@@ -41,7 +41,7 @@ passed to ``set_state``.
41
41
42
42
The ``set_state `` function accepts a ``new_state `` as its only argument and schedules a
43
43
re-render of the element where ``use_state `` was initially called. During these
44
- subsiquent re-renders the ``state `` returned by ``use_state `` will take on the value
44
+ subsequent re-renders the ``state `` returned by ``use_state `` will take on the value
45
45
of ``new_state ``.
46
46
47
47
.. note ::
@@ -56,7 +56,7 @@ Functional Updates
56
56
57
57
If the new state is computed from the previous state, you can pass a function which
58
58
accepts a single argument (the previous state) and returns the next state. Consider this
59
- simple use case of a counter where we've pulled out logic for incrementing and
59
+ simply use case of a counter where we've pulled out logic for incrementing and
60
60
decrementing the count:
61
61
62
62
.. literalinclude :: examples/use_state_counter.py
@@ -65,7 +65,7 @@ decrementing the count:
65
65
66
66
We use the functional form for the "+" and "-" buttons since the next ``count `` depends
67
67
on the previous value, while for the "Reset" button we simple assign the
68
- ``initial_count `` since it's independent of the prior ``count ``. This is a trivial
68
+ ``initial_count `` since it is independent of the prior ``count ``. This is a trivial
69
69
example, but it demonstrates how complex state logic can be factored out into well
70
70
defined and potentially reuseable functions.
71
71
@@ -103,19 +103,17 @@ use_effect
103
103
104
104
use_effect(did_render)
105
105
106
- The ``use_effect `` hook accepts a function which is may be imperative, or mutate state.
107
- The function will be called once the element has rendered, that is, when the
108
- element's render function and those of any child elements it produces have rendered
109
- successfully.
106
+ The ``use_effect `` hook accepts a function which may be imperative, or mutate state. The
107
+ function will be called immediately after the layout has fully updated.
110
108
111
- Mutations, subscriptsion , delayed actions, and other `side effects `_ can cause
109
+ Mutations, subscriptions , delayed actions, and other `side effects `_ can cause
112
110
unexpected bugs if placed in the main body of an element's render function. Thus the
113
111
``use_effect `` hook provides a way to safely escape the purely functional world of
114
112
element render functions.
115
113
116
114
.. note ::
117
115
118
- Normally in react the ``did_render `` function is called once an update has been
116
+ Normally in React the ``did_render `` function is called once an update has been
119
117
commited to the screen. Since no such action is performed by IDOM, and the time
120
118
at which the update is displayed cannot be known we are unable to achieve parity
121
119
with this behavior.
@@ -124,15 +122,16 @@ element render functions.
124
122
Cleaning Up Effects
125
123
...................
126
124
127
- If the effect you wish to enact creates resources you'll probably need to clean them up.
128
- In such cases you may simply return a function the addresses this from the function
129
- which created the resource. Consider the case of opening and then closing a connection:
125
+ If the effect you wish to enact creates resources, you'll probably need to clean them
126
+ up. In such cases you may simply return a function that addresses this from the
127
+ ``did_render `` function which created the resource. Consider the case of opening and
128
+ then closing a connection:
130
129
131
130
.. code-block ::
132
131
133
132
def establish_connection():
134
133
connection = open_connection(url)
135
- return connection.close
134
+ return lambda: close_connection( connection)
136
135
137
136
use_effect(establish_connection)
138
137
@@ -145,7 +144,7 @@ time an element renders.
145
144
Conditional Effects
146
145
...................
147
146
148
- By default effects are triggered after ever successful render to ensure that all state
147
+ By default, effects are triggered after every successful render to ensure that all state
149
148
referenced by the effect is up to date. However you can limit the number of times an
150
149
effect is fired by specifying exactly what state the effect depends on. In doing so
151
150
the effect will only occur when the given state changes:
@@ -154,7 +153,7 @@ the effect will only occur when the given state changes:
154
153
155
154
def establish_connection():
156
155
connection = open_connection(url)
157
- return connection.close
156
+ return lambda: close_connection( connection)
158
157
159
158
use_effect(establish_connection, [url])
160
159
@@ -210,12 +209,12 @@ use_callback
210
209
211
210
A derivative of :ref: `use_memo `, the ``use_callback `` hook teturns a
212
211
`memoized <memoization >`_ callback. This is useful when passing callbacks to child
213
- elements which check reference equality to prevent unnecessary renders. The of the
212
+ elements which check reference equality to prevent unnecessary renders. The of
214
213
``memoized_callback `` will only change when the given depdencies do.
215
214
216
215
.. note ::
217
216
218
- The list of "dependencies" are not passed as arguments to the function ostensibly
217
+ The list of "dependencies" are not passed as arguments to the function. Ostensibly
219
218
though, that is what they represent. Thus any variable referenced by the function
220
219
must be listed as dependencies. We're working on a linter to help enforce this
221
220
[GH202 ]_.
@@ -243,9 +242,9 @@ after) and should not incur side effects.
243
242
244
243
.. warning ::
245
244
246
- Remember that you shouldn't optimize something else you know it's a performance
247
- bottleneck. Write your code without ``use_memo `` first and then add it to later
248
- to targeted sections that need a speed-up.
245
+ Remember that you shouldn't optimize something unless you know it's a performance
246
+ bottleneck. Write your code without ``use_memo `` first and then add it to targeted
247
+ sections that need a speed-up.
249
248
250
249
.. note ::
251
250
@@ -267,9 +266,9 @@ Returns a mutable :class:`~idom.core.hooks.Ref` object that has a single
267
266
``initial_state ``. The identity of the ``Ref `` object will be preserved for the lifetime
268
267
of the element.
269
268
270
- A ``Ref `` is most useful if you need to incur side effect since updating its
269
+ A ``Ref `` is most useful if you need to incur side effects since updating its
271
270
``.current `` attribute doesn't trigger a re-render of the element. You'll often use this
272
- hook alongside :ref: `use_effect ` or in response to element event handlers. The
271
+ hook alongside :ref: `use_effect ` or in response to element event handlers.
273
272
:ref: `The Game Snake ` provides a good use case for ``use_ref ``.
274
273
275
274
0 commit comments