@@ -87,27 +87,143 @@ now let's just verify that this does in fact fix the problems from before:
8787.. example :: adding_interactivity.adding_state_variable
8888 :activate-result:
8989
90- .. _Your first hook :
9190
92- .. dropdown :: :octicon:`light-bulb;2em` Your first hook
93- :color: warning
94- :open:
91+ Your First Hook
92+ ---------------
9593
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 >`).
10199
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.
106104
107105
108- Anatomy of ``use_state ``
109- ------------------------
106+ Introduction to ``use_state ``
107+ -----------------------------
110108
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+ **User interaction **
184+
185+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
186+ :lines: 12-33
187+
188+ .. tab-item :: 6
189+
190+ .. raw :: html
191+
192+ <h2 >Event handler triggers</h2 >
193+
194+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
195+ :lines: 12-33
196+ :emphasize-lines: 6
197+
198+ .. tab-item :: 7
199+
200+ .. raw :: html
201+
202+ <h2 >Next render begins</h2 >
203+
204+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
205+ :lines: 12-33
206+ :emphasize-lines: 2
207+
208+ .. tab-item :: 8
209+
210+ .. raw :: html
211+
212+ <h2 >Next state is acquired</h2 >
213+
214+ .. literalinclude :: /_examples/adding_interactivity/adding_state_variable/app.py
215+ :lines: 12-33
216+ :emphasize-lines: 3
217+
218+ .. tab-item :: 9
219+
220+ **Repeat... **
221+
222+ ...
223+
224+ .. hint ::
225+
226+ Try clicking through the numbered tabs to each highlighted step of execution
111227
112228Multiple State Declarations
113229---------------------------
0 commit comments