Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e8ae05a

Browse files
acivitilloAlessio Civitillormorshea
authoredFeb 19, 2022
docs for components sharing state (#571)
* docs for components sharing state * Add new line to end of file * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/data.json * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/data.json * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/data.json * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/data.json * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/app.py * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/app.py * Update docs/source/adding-interactivity/components-sharing-state/_examples/filterable_list/data.json * making syncedinputs an example, moving to managing-state * fixing isort import issue in shared-component-state examples * fixing link references * fixing docker build by upgrading npm and fast-json-path * Update noxfile.py Co-authored-by: Alessio Civitillo <@acivitillo@dyvenia.com> Co-authored-by: Ryan Morshead <[email protected]>
1 parent 3748f38 commit e8ae05a

File tree

10 files changed

+344
-65
lines changed

10 files changed

+344
-65
lines changed
 

‎docs/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ WORKDIR /app/
66
# --------------
77
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
88
RUN apt-get install -yq nodejs build-essential
9-
RUN npm install -g npm@7.13.0
9+
RUN npm install -g npm@8.5.0
1010

1111
# Create Python Venv
1212
# ------------------

‎docs/source/_custom_js/package-lock.json

Lines changed: 203 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/source/adding-interactivity/components-with-state/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ it. The parent component can’t change it. This lets you add state to any compo
340340
remove it without impacting the rest of the components.
341341

342342
.. card::
343-
:link: /managing-state/shared-component-state
343+
:link: /managing-state/shared-component-state/index
344344
:link-type: doc
345345

346346
:octicon:`book` Read More

‎docs/source/managing-state/index.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ Managing State
77
keeping-components-pure
88
logical-flow-of-state
99
structuring-your-state
10-
shared-component-state
10+
shared-component-state/index
1111
when-to-reset-state
1212
writing-tests
1313

14-
Under construction 🚧
14+
15+
16+
Section 4: Shared Component State
17+
---------------------------------
18+
19+
Sometimes you want the state of two components to always change together. To do it, you
20+
need to be able to share state between those two components, to share state between
21+
componets move state to the nearest parent. In React world this is known as "lifting
22+
state up" and it is a very common thing to do. Let's look at 2 examples, also from
23+
`React <https://beta.reactjs.org/learn/sharing-state-between-components>`__,
24+
but translated to IDOM.

‎docs/source/managing-state/shared-component-state.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import json
2+
from pathlib import Path
3+
4+
from idom import component, hooks, html, run
5+
6+
7+
HERE = Path(__file__)
8+
DATA_PATH = HERE.parent / "data.json"
9+
food_data = json.loads(DATA_PATH.read_text())
10+
11+
12+
@component
13+
def FilterableList():
14+
value, set_value = hooks.use_state("")
15+
return html.p(Search(value, set_value), html.hr(), Table(value, set_value))
16+
17+
18+
@component
19+
def Search(value, set_value):
20+
def handle_change(event):
21+
set_value(event["target"]["value"])
22+
23+
return html.label(
24+
"Search by Food Name: ", html.input({"value": value, "onChange": handle_change})
25+
)
26+
27+
28+
@component
29+
def Table(value, set_value):
30+
rows = []
31+
for row in food_data:
32+
name = html.td(row["name"])
33+
descr = html.td(row["description"])
34+
tr = html.tr(name, descr, value)
35+
if value == "":
36+
rows.append(tr)
37+
else:
38+
if value.lower() in row["name"].lower():
39+
rows.append(tr)
40+
headers = html.tr(html.td(html.b("name")), html.td(html.b("description")))
41+
table = html.table(html.thead(headers), html.tbody(rows))
42+
return table
43+
44+
45+
run(FilterableList)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"name": "Sushi",
4+
"description": "Sushi is a traditional Japanese dish of prepared vinegared rice"
5+
},
6+
{
7+
"name": "Dal",
8+
"description": "The most common way of preparing dal is in the form of a soup to which onions, tomatoes and various spices may be added"
9+
},
10+
{
11+
"name": "Pierogi",
12+
"description": "Pierogi are filled dumplings made by wrapping unleavened dough around a savoury or sweet filling and cooking in boiling water"
13+
},
14+
{
15+
"name": "Shish Kebab",
16+
"description": "Shish kebab is a popular meal of skewered and grilled cubes of meat"
17+
},
18+
{
19+
"name": "Dim sum",
20+
"description": "Dim sum is a large range of small dishes that Cantonese people traditionally enjoy in restaurants for breakfast and lunch"
21+
}
22+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from idom import component, hooks, html, run
2+
3+
4+
@component
5+
def SyncedInputs():
6+
value, set_value = hooks.use_state("")
7+
return html.p(
8+
Input("First input", value, set_value),
9+
Input("Second input", value, set_value),
10+
)
11+
12+
13+
@component
14+
def Input(label, value, set_value):
15+
def handle_change(event):
16+
set_value(event["target"]["value"])
17+
18+
return html.label(
19+
label + " ", html.input({"value": value, "onChange": handle_change})
20+
)
21+
22+
23+
run(SyncedInputs)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Shared Component State
2+
======================
3+
4+
5+
Sometimes you want the state of two components to always change together. To do it, you
6+
need to be able to share state between those two components, to share state between
7+
componets move state to the nearest parent. In React world this is known as "lifting
8+
state up" and it is a very common thing to do. Let's look at 2 examples, also from
9+
`React <https://beta.reactjs.org/learn/sharing-state-between-components>`__,
10+
but translated to IDOM.
11+
12+
Synced Inputs
13+
-------------
14+
15+
In the code below the two input boxes are syncronized, this happens because they share
16+
state. The state is shared via the parent component ``SyncedInputs``. Check the ``value``
17+
and ``set_value`` variables.
18+
19+
.. idom:: _examples/synced_inputs
20+
21+
Filterable List
22+
----------------
23+
24+
In the example below the search input and the list of elements below share the
25+
same state, the state represents the food name.
26+
27+
Note how the component ``Table`` gets called at each change of state. The
28+
component is observing the state and reacting to state changes automatically,
29+
just like it would do in React.
30+
31+
.. idom:: _examples/filterable_list
32+
33+
.. note::
34+
35+
Try typing a food name in the search bar.

‎src/client/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.