Skip to content

Commit 635bb67

Browse files
committed
#46 reviewed and readme_es created
1 parent e6f44fa commit 635bb67

File tree

10 files changed

+366
-199
lines changed

10 files changed

+366
-199
lines changed

03 State/.babelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"modules": false
7+
}
8+
],
9+
"react"
10+
]
11+
}

03 State/package.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
{
2-
"name": "samplereact",
2+
"name": "sample-es6",
33
"version": "1.0.0",
44
"description": "In this sample we are going to setup the basic plumbing to \"build\" our project and launch it in a dev server.",
55
"main": "index.js",
66
"scripts": {
7-
"start": "webpack-dev-server --inline",
7+
"start": "webpack-dev-server --mode development --inline --hot --open",
8+
"build": "webpack --mode development",
89
"test": "echo \"Error: no test specified\" && exit 1"
910
},
1011
"author": "",
1112
"license": "ISC",
1213
"devDependencies": {
13-
"babel-core": "^6.18.2",
14-
"babel-loader": "^6.2.7",
15-
"babel-plugin-transform-runtime": "^6.15.0",
16-
"babel-preset-es2015": "^6.18.0",
17-
"babel-preset-react": "^6.16.0",
18-
"css-loader": "^0.25.0",
19-
"file-loader": "^0.9.0",
20-
"html-webpack-plugin": "^2.24.1",
21-
"style-loader": "^0.13.1",
22-
"url-loader": "^0.5.7",
23-
"webpack": "^1.13.3",
24-
"webpack-devserver": "0.0.6"
14+
"babel-core": "^6.26.0",
15+
"babel-loader": "^7.1.4",
16+
"babel-preset-env": "^1.6.1",
17+
"babel-preset-react": "^6.24.1",
18+
"css-loader": "^0.28.11",
19+
"file-loader": "^1.1.11",
20+
"html-webpack-plugin": "^3.2.0",
21+
"mini-css-extract-plugin": "^0.4.0",
22+
"url-loader": "^1.0.1",
23+
"webpack": "^4.6.0",
24+
"webpack-cli": "^2.0.14",
25+
"webpack-dev-server": "3.1.0"
2526
},
2627
"dependencies": {
27-
"bootstrap": "^3.3.7",
28-
"react": "^15.3.2",
29-
"react-dom": "^15.3.2"
28+
"bootstrap": "^4.1.0",
29+
"react": "^16.3.2",
30+
"react-dom": "^16.3.2"
3031
}
3132
}

03 State/readme.md

Lines changed: 118 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
In this sample we will 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 will provide a default username but let the user update it.
76

8-
We will take as a starting point sample _02 Properties_:
7+
We will take as a starting point sample _[02 Properties](../02%20Properties/)_:
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
15-
_App_ component.
16-
- Update _main.jsx_ 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
19-
using a function from _App_.
11+
- Create an _`App`_ component that will hold the state, this state will contain the current username (by default assigned to "defaultUserName" value).
12+
This _App_ component will render the _`Hello`_ component. At first we will create a simple stateless _`App`_ component.
13+
- Update _[main.jsx](./src/main.jsx)_ file to include our _`App`_ component.
14+
- Change _`App`_ component to a statful class component to hold the _`userName`_ state.
15+
- Create a _`NameEdit`_ component to let the user change the username. This will change the _`App`_ state using a function from _`App`_.
2016
- Check everything is working properly.
2117

2218
## Prerequisites
@@ -27,68 +23,73 @@ Install [Node.js and npm](https://nodejs.org) if they are not already installed
2723
2824
## Steps to build it
2925

30-
- Copy the content from _02 Properties_ and execute:
26+
- Copy the content from _[02 Properties](../02%20Properties/)_ and execute:
3127

32-
```
33-
npm install
34-
```
35-
36-
- Let's create an _App_ component under a new file named _app.jsx_ (this component will display the _Hello_ component).
28+
```bash
29+
npm install
30+
```
3731

38-
```jsx
39-
export const HelloComponent = props => (
40-
<h2>Hello user: {props.userName}!</h2>
41-
);
42-
43-
HelloComponent.propTypes = {
44-
userName: React.PropTypes.string.isRequired,
45-
};
46-
47-
```
32+
- Let's create an _`App`_ component under a new file named _[app.jsx](./src/app.jsx)_ (this component will display the _`Hello`_ component).
4833

49-
- Let's update _main.jsx_ just to use the _App_ component that we have recently created.
34+
_[app.jsx](./src/app.jsx)_
35+
```jsx
36+
import React from 'react';
37+
import { HelloComponent } from './hello';
5038

51-
```jsx
52-
/* global document */
39+
export class App extends React.Component {
40+
render() {
41+
return (
42+
<div>
43+
<HelloComponent userName="John" />
44+
</div>
45+
);
46+
}
47+
}
48+
```
5349

54-
import React from 'react';
55-
import { render } from 'react-dom';
56-
import { App } from './app';
50+
- Let's update _[main.jsx](./src/main.jsx)_ just to use the _App_ component that we have recently created.
5751

58-
render(
59-
<App />
60-
, document.getElementById('root')
61-
);
52+
_[main.jsx](./src/main.jsx)_
53+
```diff
54+
import React from 'react';
55+
import {render} from 'react-dom';
56+
-- import {HelloComponent} from './hello.jsx';
57+
++ import { App } from './app';
6258

63-
```
59+
render(
60+
-- <HelloComponent userName="John" />,
61+
++ <App />,
62+
document.getElementById('root')
63+
);
64+
```
6465

6566
- Now we can check that things are still working as expected (nothing broken so far).
6667

67-
```
68-
npm start
69-
```
68+
```bash
69+
npm start
70+
```
7071

71-
- It's time to revisit _app.jsx_, since we want to store the name of the user and let the
72-
user updated it, let's move this component to a class stateful component and define
73-
a state, including _userName_ and pass this value to the _Hello_ component.
72+
- It's time to revisit _[app.jsx](./src/app.jsx)_, since we want to store the name of the user 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.
7473

75-
```jsx
76-
import React from 'react';
77-
import { HelloComponent } from './hello';
74+
_[app.jsx](./src/app.jsx)_
75+
```diff
76+
import React from 'react';
77+
import { HelloComponent } from './hello';
7878

79-
export class App extends React.Component {
80-
constructor(props) {
81-
super(props);
79+
export class App extends React.Component {
80+
++ constructor(props) {
81+
++ super(props);
8282

83-
this.state = {
84-
userName: 'defaultUserName',
85-
};
86-
}
83+
++ this.state = {
84+
++ userName: 'defaultUserName',
85+
++ };
86+
++ }
8787

8888
render() {
8989
return (
9090
<div>
91-
<HelloComponent userName={this.state.userName} />
91+
-- <HelloComponent userName="John" />
92+
++ <HelloComponent userName={this.state.userName} />
9293
</div>
9394
);
9495
}
@@ -98,72 +99,72 @@ a state, including _userName_ and pass this value to the _Hello_ component.
9899

99100
- Again, we can do a quick check to test that everything is working as expected.
100101

101-
```
102-
npm start
103-
```
104-
105-
- Now it's time to create an _NameEdit_ component, this component will let the user
106-
update his username and will notify with a callback to the parent control whenever
107-
the _userName_ gets updated.
108-
109-
```jsx
110-
import React from 'react';
111-
112-
export const NameEditComponent = props => (
113-
<div>
114-
<label htmlFor="userName">Update Name:</label>
115-
<input id="userName" value={props.userName} onChange={props.onChange} />
116-
</div>
117-
);
118-
119-
NameEditComponent.propTypes = {
120-
userName: React.PropTypes.string.isRequired,
121-
onChange: React.PropTypes.func.isRequired,
122-
};
123-
124-
```
125-
126-
- In the _app.jsx_ file let's add a function to set the changed _userName_ in the state.
127-
128-
```jsx
129-
import React from 'react';
130-
import { HelloComponent } from './hello';
131-
132-
export class App extends React.Component {
133-
constructor(props) {
134-
super(props);
135-
136-
this.state = {
137-
userName: 'defaultUserName',
138-
};
139-
140-
this.setUsernameState = this.setUsernameState.bind(this);
141-
}
142-
143-
setUsernameState(event) {
144-
// If the state gets more complex we should use object.assign
145-
this.setState({
146-
userName: event.target.value,
147-
});
148-
}
102+
```bash
103+
npm start
104+
```
105+
106+
- Now it's time to create an _`NameEdit`_ component, this component will let the user update his username and will notify with a callback to the parent control whenever the _`userName`_ gets updated.
107+
108+
_[nameEdit.jsx](./src/nameEdit.jsx)_
109+
```jsx
110+
import React from 'react';
111+
import PropTypes from 'prop-types';
112+
113+
export const NameEditComponent = (props) => (
114+
<div>
115+
<label htmlFor="userName">Update Name:</label>
116+
<input id="userName" value={props.userName} onChange={props.onChange} />
117+
</div>
118+
);
119+
120+
NameEditComponent.propTypes = {
121+
userName: PropTypes.string.isRequired,
122+
onChange: PropTypes.func.isRequired,
123+
};
124+
```
125+
126+
- In the _[app.jsx](./src/app.jsx)_ file let's add a function to set the changed _`userName`_ in the state.
127+
128+
_[app.jsx](./src/app.jsx)_
129+
```diff
130+
import React from 'react';
131+
import { HelloComponent } from './hello';
132+
133+
export class App extends React.Component {
134+
constructor(props) {
135+
super(props);
136+
137+
this.state = {
138+
userName: 'defaultUserName',
139+
};
140+
141+
++ this.setUsernameState = this.setUsernameState.bind(this);
142+
}
149143

150-
render() {
151-
return (
152-
<div>
153-
<HelloComponent userName={this.state.userName} />
154-
<NameEditComponent
155-
userName={this.state.userName}
156-
onChange={this.setUsernameState}
157-
/>
158-
</div>
159-
);
160-
}
144+
++ setUsernameState(event) {
145+
++ // If the state gets more complex we should use object.assign
146+
++ this.setState({
147+
++ userName: event.target.value,
148+
++ });
149+
++ }
150+
151+
render() {
152+
return (
153+
<div>
154+
<HelloComponent userName={this.state.userName} />
155+
++ <NameEditComponent
156+
++ userName={this.state.userName}
157+
++ onChange={this.setUsernameState}
158+
++ />
159+
</div>
160+
);
161161
}
162+
}
162163

163164
```
164165

165166
- Finally let's test the final sample.
166167

167-
```
168-
npm start
169-
```
168+
```bash
169+
npm start
170+
```

0 commit comments

Comments
 (0)