@@ -87,27 +87,145 @@ now let's just verify that this does in fact fix the problems from before:
87
87
.. example :: adding_interactivity.adding_state_variable
88
88
:activate-result:
89
89
90
- .. _Your first hook :
91
90
92
- .. dropdown :: :octicon:`light-bulb;2em` Your first hook
93
- :color: warning
94
- :open:
91
+ Your First Hook
92
+ ---------------
95
93
96
- In IDOM, ``use_state ``, as well as any other function whose name starts with
97
- `` use ``, is called a "hook". These are special functions that should only be called
98
- while IDOM is :ref: `rendering <the- rendering- process >`. They let you "hook into" the
99
- different capabilities of IDOM's components of which ``use_state `` is just one (well
100
- get into the other :ref: `later <managing state >`).
94
+ In IDOM, ``use_state ``, as well as any other function whose name starts with `` use ``, is
95
+ called a "hook". These are special functions that should only be called while IDOM is
96
+ :ref: `rendering <the rendering process >`. They let you "hook into" the different
97
+ capabilities of IDOM's components of which ``use_state `` is just one (well get into the
98
+ other :ref: `later <managing state >`).
101
99
102
- While hooks are just normal functions, but it's helpful to think of them as
103
- :ref: `unconditioned <rules of hooks >` declarations about a component's needs. In
104
- other words, you'll "use" hooks at the top of your component in the same way you
105
- might "import" modules at the top of your Python files.
100
+ While hooks are just normal functions, but it's helpful to think of them as
101
+ :ref: `unconditioned <rules of hooks >` declarations about a component's needs. In other
102
+ words, you'll "use" hooks at the top of your component in the same way you might
103
+ "import" modules at the top of your Python files.
106
104
107
105
108
- Anatomy of ``use_state ``
109
- ------------------------
106
+ Introduction to ``use_state ``
107
+ -----------------------------
110
108
109
+ When you call :func: `~idom.core.hooks.use_state ` inside the body of a component's render
110
+ function, you're declaring that this component needs to remember something. That
111
+ "something" which needs to be remembered, is known as **state **. So when we look at an
112
+ assignment expression like the one below
113
+
114
+ .. code-block ::
115
+
116
+ index, set_index = use_state(0)
117
+
118
+ we should read it as saying that ``index `` is a piece of state which must be
119
+ remembered by the component that declared it. The argument to ``use_state `` (in this
120
+ case ``0 ``) is then conveying what the initial value for ``index `` is.
121
+
122
+ We should then understand that each time the component which owns this state renders
123
+ ``use_state `` will return a tuple containing two values - the current value of the state
124
+ (``index ``) and a function to change that value the next time the component is rendered.
125
+ Thus, in this example:
126
+
127
+ - ``index `` - is a **state variable ** containing the currently stored value.
128
+ - ``set_index `` - is a **state setter ** for changing that value and triggering a re-render
129
+ of the component.
130
+
131
+ .. note ::
132
+
133
+ The convention is that, if you name your state variable ``thing ``, your state setter
134
+ should be named ``set_thing ``. While you could name them anything you want,
135
+ adhereing to the convention makes things easier to understand across projects.
136
+
137
+ To understand how this works in context, let's break down our example:
138
+
139
+ .. tab-set ::
140
+
141
+ .. tab-item :: 1
142
+
143
+ .. raw :: html
144
+
145
+ <h2 >Initial render</h2 >
146
+
147
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
148
+ :lines: 12-33
149
+ :emphasize-lines: 2
150
+
151
+ .. tab-item :: 2
152
+
153
+ .. raw :: html
154
+
155
+ <h2 >Initial state declaration</h2 >
156
+
157
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
158
+ :lines: 12-33
159
+ :emphasize-lines: 3
160
+
161
+ .. tab-item :: 3
162
+
163
+ .. raw :: html
164
+
165
+ <h2 >Define event handler</h2 >
166
+
167
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
168
+ :lines: 12-33
169
+ :emphasize-lines: 5
170
+
171
+ .. tab-item :: 4
172
+
173
+ .. raw :: html
174
+
175
+ <h2 >Return the view</h2 >
176
+
177
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
178
+ :lines: 12-33
179
+ :emphasize-lines: 16
180
+
181
+ .. tab-item :: 5
182
+
183
+ .. raw :: html
184
+
185
+ <h2 >User interaction</h2 >
186
+
187
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
188
+ :lines: 12-33
189
+
190
+ .. tab-item :: 6
191
+
192
+ .. raw :: html
193
+
194
+ <h2 >Event handler triggers</h2 >
195
+
196
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
197
+ :lines: 12-33
198
+ :emphasize-lines: 6
199
+
200
+ .. tab-item :: 7
201
+
202
+ .. raw :: html
203
+
204
+ <h2 >Next render begins</h2 >
205
+
206
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
207
+ :lines: 12-33
208
+ :emphasize-lines: 2
209
+
210
+ .. tab-item :: 8
211
+
212
+ .. raw :: html
213
+
214
+ <h2 >Next state is acquired</h2 >
215
+
216
+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
217
+ :lines: 12-33
218
+ :emphasize-lines: 3
219
+
220
+ .. tab-item :: 9
221
+
222
+ **Repeat... **
223
+
224
+ ...
225
+
226
+ .. hint ::
227
+
228
+ Try clicking through the numbered tabs to each highlighted step of execution
111
229
112
230
Multiple State Declarations
113
231
---------------------------
0 commit comments