Skip to content

Commit 2d3ba23

Browse files
authored
Merge pull request #129 from AurelioGomezRosales/patch-5
Update README.md
2 parents 64f5d82 + c812e0a commit 2d3ba23

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

03 State/readme.md

+40-27
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
# 03 State
22

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.
44

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.
76

8-
We will take as a starting point sample _02 Properties_:
7+
We take as a starting point the example _02 Properties_:
98

109
## Summary steps:
1110

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
1514
_App_ component.
1615
- 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
1918
using a function from _App_.
20-
- Check everything is working properly.
19+
- Check everything works properly.
2120

2221
## Prerequisites
2322

@@ -44,7 +43,7 @@ export const App = () => {
4443
}
4544
```
4645

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.
4847

4948
_./src/main.tsx_
5049

@@ -60,15 +59,13 @@ _./src/main.tsx_
6059
);
6160
```
6261

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.
6463

6564
```
6665
npm start
6766
```
6867

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.
7269

7370
_./src/app.tsx_
7471

@@ -98,15 +95,13 @@ export class App extends React.Component<Props, State> {
9895
}
9996
```
10097

101-
- 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.
10299

103100
```
104101
npm start
105102
```
106103

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.
110105

111106
_./src/nameEdit.tsx_
112107

@@ -118,20 +113,26 @@ interface Props {
118113
onChange : (event) => void;
119114
}
120115

121-
export const NameEditComponent = (props : Props) => {
122-
return (
116+
export const NameEditComponent = (props : Props) =>
123117
<>
124118
<label>Update name:</label>
125119
<input value={props.userName} onChange={props.onChange}/>
126120
<>
127-
);
128-
}
129121
```
130-
> What is this Fragment or <> stuff? A way to create component that have multiple root elements (not a single parent)
131122

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:
132124

125+
```jsx
126+
...
127+
export const NameEditComponent = (props : Props) =>
128+
<React.Fragment>
129+
<label>Update name:</label>
130+
<input value={props.userName} onChange={props.onChange}/>
131+
</React.Fragment>
132+
}
133+
```
133134

134-
- In the _app.tsx_ file let's add a function to set the changed _userName_ in the state.
135+
- In the _app.tsx_ file, let's add a function to replace the state value of _userName_ with the new one.
135136

136137
```diff
137138
import * as React from 'react';
@@ -167,9 +168,21 @@ export const NameEditComponent = (props : Props) => {
167168
}
168169
```
169170

170-
> 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
174+
175+
```
176+
setUserNameState = (newName: string) => {
177+
this.setState({userName: newName}, this.nameChanged);
178+
}
179+
180+
nameChanged() {
181+
/* logic here gets invoked after the new name value in the state is set. */
182+
}
183+
```
171184

172-
- Finally let's test the final sample.
185+
- Finally let's test everything works once more.
173186

174187
```
175188
npm start

0 commit comments

Comments
 (0)