Skip to content

Commit 03712c3

Browse files
committed
start work on dangers of mutability section
1 parent aad455a commit 03712c3

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def MovingDot():
6+
position, set_position = use_state({"x": 0, "y": 0})
7+
8+
def handle_click(event):
9+
set_position(
10+
{
11+
"x": event["clientX"],
12+
"y": event["clientY"],
13+
}
14+
)
15+
16+
return html.div(
17+
{
18+
"onClick": handle_click,
19+
"style": {
20+
"position": "relative",
21+
"height": "200px",
22+
"width": "100%",
23+
"backgroundColor": "white",
24+
},
25+
},
26+
html.div(
27+
{
28+
"style": {
29+
"position": "absolute",
30+
"backgroundColor": "red",
31+
"borderRadius": "50%",
32+
"width": "20px",
33+
"height": "20px",
34+
"left": "-10px",
35+
"top": "-10px",
36+
"transform": f"translate({position['x']}px, {position['y']}px)",
37+
},
38+
}
39+
),
40+
)
41+
42+
43+
run(MovingDot)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def MovingDot():
6+
position, set_position = use_state({"x": 0, "y": 0})
7+
8+
def handle_click(event):
9+
position["x"] = event["clientX"]
10+
position["y"] = event["clientY"]
11+
set_position(position)
12+
13+
return html.div(
14+
{
15+
"onClick": handle_click,
16+
"style": {
17+
"position": "relative",
18+
"height": "200px",
19+
"width": "100%",
20+
"backgroundColor": "white",
21+
},
22+
},
23+
html.div(
24+
{
25+
"style": {
26+
"position": "absolute",
27+
"backgroundColor": "red",
28+
"borderRadius": "50%",
29+
"width": "20px",
30+
"height": "20px",
31+
"left": "-10px",
32+
"top": "-10px",
33+
"transform": f"translate({position['x']}px, {position['y']}px)",
34+
},
35+
}
36+
),
37+
)
38+
39+
40+
run(MovingDot)
Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
1-
.. _Dangers of Mutability:
1+
Dangers of Mutability
2+
=====================
23

3-
Dangers of Mutability 🚧
4-
========================
4+
While state can hold any type of value, you should be careful to avoid directly
5+
modifying objects that you declare as state with IDOM. In other words, you must not
6+
:ref:`"mutate" <What is a Mutation>` values which are held as state. Rather, to change
7+
these values you should use new ones or create copies.
8+
9+
10+
.. _what is a mutation:
11+
12+
What is a Mutation?
13+
-------------------
14+
15+
In Python, values may be either "mutable" or "immutable". Mutable objects are those
16+
whose underlying data can be changed after they are created, and immutable objects are
17+
those which cannot. A "mutation" then, is the act of changing the underlying data of a
18+
mutable value. For example, a :class:`dict` is a mutable type of value. In the code
19+
below, an initially empty dictionary is created. Then, a key and value is added to it:
20+
21+
.. code-block::
22+
23+
x = {}
24+
x["a"] = 1
25+
assert x == {"a": 1}
26+
27+
This is different from something like a :class:`str` which is immutable. Instead of
28+
modifying the underlying data of an existing value, a new one must be created to
29+
facilitate change:
30+
31+
x = "Hello"
32+
y = x + " world!"
33+
assert x is not y
534

635
.. note::
736

8-
Under construction 🚧
37+
In Python, the ``is`` and ``is not`` operators check whether two values are
38+
identitcal. This `is distinct
39+
<https://realpython.com/python-is-identity-vs-equality>`__ from checking whether two
40+
values are equivalent with the ``==`` or ``!=`` operators.
41+
42+
Thus far, all the values we've been working with have been immutable. These include
43+
:class:`int`, :class:`float`, :class:`str`, and :class:`bool` values. As a result, we've
44+
have not had to consider the consiquences of mutations.
45+
46+
47+
Why Avoid Mutation?
48+
-------------------
49+
50+
Unfortunately, IDOM does not understand that when a value is mutated, it may have
51+
changed. As a result, mutating values will not trigger re-renders. Thus, you must be
52+
careful to avoid mutation whenever you want IDOM to re-render a component.
53+
54+
.. idom:: _examples/moving_dot

0 commit comments

Comments
 (0)