You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 03 State/readme.md
+40-27
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,22 @@
1
1
# 03 State
2
2
3
-
In this sample we will introduce a basic React concept, handling State.
3
+
In this example we introduce a basic React concept: handling State.
4
4
5
-
In this scenario we will provide a default username but let the user update
6
-
it.
5
+
In this scenario we provide a default username and let the user update it.
7
6
8
-
We will take as a starting point sample_02 Properties_:
7
+
We take as a starting point the example_02 Properties_:
9
8
10
9
## Summary steps:
11
10
12
-
- Create an _App_ component that will hold the state, this state will contain the current
13
-
username (by default assigned to "defaultUserName" value).
14
-
This _App_ component will render the _Hello_ component. At first we will create a simple stateless
11
+
- Create an _App_ component that holds the state. This state will contain the current
12
+
username (with default value "defaultUserName").
13
+
This _App_ component renders the _Hello_ component. At first we create a simple stateless
15
14
_App_ component.
16
15
- Update _main.tsx_ file to include our _App_ component.
17
-
- Change _App_ component to a statful class component to hold the _userName_ state.
18
-
- Create a _NameEdit_ component to let the user change the username. This will change the _App_ state
16
+
- Change _App_ component to a stateful class component to hold the _userName_ state.
17
+
- Create a _NameEdit_ component to let the user change the value of username. This changes the _App_ state
19
18
using a function from _App_.
20
-
- Check everything is working properly.
19
+
- Check everything works properly.
21
20
22
21
## Prerequisites
23
22
@@ -44,7 +43,7 @@ export const App = () => {
44
43
}
45
44
```
46
45
47
-
- Let's update _main.tsx_ just to use the _App_ component that we have recently created.
46
+
- Let's update _main.tsx_ just to use the _App_ component that we have just created.
48
47
49
48
_./src/main.tsx_
50
49
@@ -60,15 +59,13 @@ _./src/main.tsx_
60
59
);
61
60
```
62
61
63
-
- Now we can check that things are still working as expected (nothing broken so far).
62
+
- Now we can check that things are still working as expected.
64
63
65
64
```
66
65
npm start
67
66
```
68
67
69
-
- It's time to revisit _app.tsx_, since we want to store the name of the user and let the
70
-
user updated it, let's move this component to a class stateful component and define
71
-
a state, including _userName_ and pass this value to the _Hello_ component.
68
+
- It's time to revisit _app.tsx_. We want to store the user's name and let the user updated it. Let's move this component to a class stateful component and define a state including _userName_, and pass this value to the _Hello_ component.
- Again, we can do a quick check to test that everything is working as expected.
98
+
- Again, we can do a quick check to test that everything works as expected.
102
99
103
100
```
104
101
npm start
105
102
```
106
103
107
-
- Now it's time to create an _NameEdit_ component, this component will let the user
108
-
update his username and will notify with a callback to the parent control whenever
109
-
the _userName_ gets updated.
104
+
- Now it's time to create an _NameEdit_ component. This component lets the user update his username and notifies with a callback to the parent control whenever the value of _userName_ gets updated.
> What is this Fragment or <> stuff? A way to create component that have multiple root elements (not a single parent)
131
122
123
+
Side note: What is this Fragment or <> stuff? A way to create component that has multiple root elements (not a single parent). Available from React 16.2. As an alternative you can type:
> Note down the fat arrow class method, this will avoid loosing the _this_ context on the callback
171
+
Side note: mind the use of the fat arrow function. This avoids losing the context for _this_ in the callback.
172
+
173
+
Side note 2: this.setState() will change the value of the state at some point in the future. Do not consider it is a synchronous change - it isn't. Writing logic that depends on the username new value being in the state right after calling this.setState() is wrong and might fail. If you need to write code dependent on the new value being in the state, use a callback as second parameter of this.setState(). See this example
0 commit comments