Skip to content

Commit 0982d1f

Browse files
committed
Review hooks samples
1 parent e7910d4 commit 0982d1f

File tree

27 files changed

+151
-156
lines changed

27 files changed

+151
-156
lines changed

hooks/00_BoilerPlate/Readme.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Summary steps:
1515
- Webpack and webpack-dev-server.
1616
- TypeScript.
1717
- Babel.
18-
- Bootstrap.
1918
- Setup **[./webpack.config.js](./webpack.config.js)**
2019
- Create a test JS file.
2120
- Create a simple HTML file.
@@ -136,8 +135,7 @@ _[./package.json](./package.json)_
136135
"main": "index.js",
137136
"scripts": {
138137
"start": "webpack-dev-server --mode development --inline --hot --open",
139-
"build": "webpack --mode development",
140-
"test": "echo \"Error: no test specified\" && exit 1"
138+
"build": "webpack --mode development"
141139
},
142140
"author": "",
143141
"license": "ISC",
@@ -198,7 +196,6 @@ _[./src/index.html](./src/index.html)_
198196
- Now it's time to create a basic **[./webpack.config.js](./webpack.config.js)** file. This configuration includes plumbing for:
199197
- Launching a web dev server.
200198
- Transpiling from TypeScript to JavaScript.
201-
- Setting up Twitter Bootstrap (including fonts, etc...).
202199
- Generating the build under a **dist** folder.
203200

204201
_[./webpack.config.js](./webpack.config.js)_

hooks/01_HelloReact/Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ _[./src/index.tsx](./src/index.tsx)_
9696

9797
- Delete the file _main.ts_ we are not going to need it anymore.
9898

99-
- Modify the [./webpack.config.js](./webpack.config.js) file and change the entry point from `./index.ts`
99+
- Modify the [./webpack.config.js](./webpack.config.js) file and change the entry point from `./main.ts`
100100
to `./index.tsx`.
101101

102102
_[./webpack.config.js](./webpack.config.js)_
@@ -110,8 +110,8 @@ module.exports = {
110110
extensions: ['.js', '.ts', '.tsx']
111111
},
112112
entry: ["@babel/polyfill",
113-
- "main.ts"],
114-
+ "./index.tsx"],
113+
- "./main.ts"],
114+
+ "./index.tsx"],
115115
output: {
116116
path: path.join(basePath, "dist"),
117117
filename: "bundle.js"

hooks/02_Properties/Readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import * as React from 'react';
3838

3939
- export const HelloComponent = () => {
4040
+ export const HelloComponent = (props: Props) => (
41-
- {
4241
- return (
4342
- <h2>Hello component !</h2>
4443
+ <h2>Hello user: { props.userName } !</h2>

hooks/03_State/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We take as a starting point the example _02 Properties_:
1212
username (with default value "defaultUserName").
1313
This _App_ component renders the _Hello_ component. At first we create a simple stateless
1414
_App_ component.
15-
- Update _main.tsx_ file to include our _App_ component.
15+
- Update _index.tsx_ file to include our _App_ component.
1616
- Change _App_ component to a stateful class component to hold the _userName_ state.
1717
- Create a _NameEdit_ component to let the user change the value of username. This changes the _App_ state
1818
using a function from _App_.
@@ -67,7 +67,7 @@ _./src/index.tsx_
6767

6868
- It's time to revisit _app.tsx_. We want to store the user's name and let the user updated it. We will use hooks to
6969
allow _App_ functional components to make use of state (this works in React 16.8.2 and above if you have to use
70-
older verions you have to use a class component, check the "old*classes_components" on the root of this repo for example).
70+
older versions you have to use a class component, check the "old*classes_components" on the root of this repo for example).
7171
We will add \_userName* to the state.
7272

7373
Let's move this component to a class stateful component and define a state including _userName_, and pass this value to the _Hello_ component.

hooks/05_Refactor/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ We can think about two possible solutions:
6262

6363
- The child components remains as pure presentational.
6464
- You have only one place where the updates is being done, and you have control over it, you could easily change behavior
65-
e.g. if the user has alreay started typing then name then ignore the ajax call.
65+
e.g. if the user has already started typing the name then ignore the ajax call.
6666

6767
We will stick with the second approach in the rest of examples, but we encourage you to go through both approaches as
6868
a learning excercise.
@@ -119,7 +119,7 @@ The first approach was fine if we come from a Java / C# mindset, but it has it d
119119
what would happen if we want the editingName to be preserved if it has changed (async call
120120
would have no effect then)? When there is a conflict updating the state between parent and child
121121
a valid solution is to lift up that part of the state to the parent component, if we follow
122-
this approach we can endup having the core state isolated in container components (prior step
122+
this approach we can end up having the core state isolated in container components (prior step
123123
to build redux on top of it).
124124

125125
Let's see how can we lift up this state:

hooks/06_Enable/Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Let's continue with the update name sample, this time we want to disable the
44
"update" button when the input is empty or when the value hasn't changed.
55

6-
We will take a startup point sample _05 Refactor_
6+
We will take as startup point sample _05 Refactor_
77

88
## Summary steps:
99

@@ -51,7 +51,7 @@ _./src/nameEdit.tsx_
5151
decide in which cases it should be disabled (thanks to [Victor Borrego](https://github.com/v-borrego) to point out this great solution).
5252

5353
We will follow the second approach since is the one that can provide more flexibility to the
54-
control (in a real project, choosing between one approach or the other depends on specfication details).
54+
control (in a real project, choosing between one approach or the other depends on specification details).
5555

5656
We will expose a _disabled_ property in the _NameEdit_ component.
5757

@@ -97,7 +97,7 @@ npm start
9797

9898
# Excercise
9999

100-
Ideas to further implement this, let's convert our NamEdit component to a password
100+
Ideas to further implement this, let's convert our NameEdit component to a password
101101
strength component:
102102

103103
- Extract the disabled condition to function.

hooks/07_ColorPicker/Readme.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# 07 Colopicker
22

3-
In this sample we are going to implement a color picker component, it will allow us to choose
4-
between red / green / blue components and displaty the resulting color.
3+
In this sample we are going to implement a color picker component, it will allow us to choose between red / green / blue components and display the resulting color.
54

6-
We will start with a non optimal implementation and we will refactor it in sample 09.
5+
We will start with a non optimal implementation and we will refactor it in sample 08.
76

87
# Steps
98

109
- We will take as starting point sample _06 Enable_, copy the content from that project and
11-
exectue _npm install_
10+
execute _npm install_
1211

1312
```bash
1413
npm install
@@ -18,7 +17,7 @@ npm install
1817

1918
- We will create a components folder under _src_ (_src/components_).
2019

21-
- Inside that components folder we will copy all the components that we have created (_nameEdit_
20+
- Inside that components folder we will copy all the components that we have created (_nameEdit.tsx_
2221
and _hello.tsx_).
2322

2423
- We will create an _index_ file under _src/components_ and create a barrel (by doing this, later
@@ -47,7 +46,7 @@ export interface Color {
4746

4847
- Let's start by creating a _ColorBrowser_ component: this color will just display the
4948
selected color (under the hood is just a div, by applying css styling we provide a width
50-
and height to that rectanble, and a background color).
49+
and height to that rectangle, and a background color).
5150

5251
_./src/components/colorBrowser.tsx_
5352

@@ -128,7 +127,7 @@ _./src/app.tsx_
128127
npm start
129128
```
130129

131-
- We want add color picker editing capabilities, let's create a color picker component
130+
- We want to add color picker editing capabilities, let's create a color picker component
132131
and add a single slider for one of the color values.
133132

134133
_./src/components/colorpicker.tsx_

hooks/08_ColorPickerRefactor/Readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# 08 Colorpicker Refactor
22

3-
In this example we are going to review the colorpicker component we have created and simplify it. Right now we have three slider controls with many details that make our HTML hard to read. Let's componentize this scenario.
3+
In this example we are going to review the colorPicker component we have created and simplify it. Right now we have three slider controls with many details that make our HTML hard to read. Let's componentize this scenario.
44

5-
We take _07 Colorpicker_ as reference.
5+
We take _07 ColorPicker_ as reference.
66

77
# Steps to reproduce this sample
88

9-
- Copy the content from _07 Colorpicker_ and execute _npm install_.
9+
- Copy the content from _07 ColorPicker_ and execute _npm install_.
1010

1111
```bash
1212
npm install
@@ -37,9 +37,9 @@ _DO NOT COPY THIS_
3737

3838
- We will start our refactor by componentizing this piece of code,
3939
let's append to the _colorPicker.tsx_ the following component
40-
(another approach could be to create a new file, that's a tetchty decisition,
40+
(another approach could be to create a new file, that's a tetchy decision,
4141
on one hand it makes sense to keep in the same file the slider since is quite
42-
related to the picker, on the other segregating in diferent files is another
42+
related to the picker, on the other segregating in different files is another
4343
valid approach).
4444

4545
_./src/components/colorPicker.tsx_

hooks/09_Sidebar/Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ implementation, then we will make it generic.
1212
npm install
1313
```
1414

15-
- Create a file called _src/sidebar.css_ and add the following styles (http://www.w3schools.com/howto/howto_js_sidenav.asp):
15+
- Create a file called _src/components/sidebar.css_ and add the following styles (http://www.w3schools.com/howto/howto_js_sidenav.asp):
1616

1717
_./src/components/sidebar.css_
1818

@@ -100,10 +100,10 @@ _./webpack.config.js_
100100

101101
```
102102

103-
- We are going to create now a sidebar component, _src/sidebar.tsx_. Right now we will create just
103+
- We are going to create now a sidebar component, _src/components/sidebar.tsx_. Right now we will create just
104104
a rectangle and we will interact with the animation.
105105

106-
We need to install node typings, since we are goin to make use of _require_ to import from
106+
We need to install node typings, since we are going to make use of _require_ to import from
107107
the _css_.
108108

109109
```bash
@@ -212,7 +212,7 @@ export const SidebarComponent = (props: Props) =>
212212
</div>
213213
```
214214

215-
- Let's make a quick test we will show always the side bar:
215+
- Let's make a quick test, we will show always the side bar:
216216

217217
_./src/app.tsx_
218218

hooks/09_Sidebar/Readme_es.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ En este ejemplo vamos a añadir una barra lateral a nuestra aplicación, empezar
1010
npm install
1111
```
1212

13-
- Cree un archivo llamado _src/sidebar.css_ y añada los siguientes estilos (http://www.w3schools.com/howto/howto_js_sidenav.asp):
13+
- Cree un archivo llamado _src/components/sidebar.css_ y añada los siguientes estilos (http://www.w3schools.com/howto/howto_js_sidenav.asp):
1414

1515
_./src/components/sidebar.css_
1616

@@ -98,7 +98,7 @@ _./webpack.config.js_
9898

9999
```
100100

101-
- Vamos a crear el componente de la barra lateral, _src/sidebar.tsx_. Crearemos sólo
101+
- Vamos a crear el componente de la barra lateral, _src/components/sidebar.tsx_. Crearemos sólo
102102
un rectángulo e interactuaremos con la animación.
103103

104104
Necesitamos instalar los tipos para _node_, ya que usaremos _require_ a la hora de importar desde el _css_.

hooks/10_TableMock/Readme.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
In this sample we are going to show a table with mock data.
44

55
We will simulate that this method is calling an ajax api (it will return a promise),
6-
by doing this we can check how easy is to replace it by a reall call in the next
6+
by doing this we can check how easy is to replace it by a real call in the next
77
example.
88

99
# Steps to reproduce the sample
@@ -15,8 +15,6 @@ example.
1515
npm install
1616
```
1717

18-
- Let's define a model entity:
19-
2018
- Let's define a model entity in _src/model/member.ts_:
2119

2220
_./src/model/member.ts_
@@ -65,7 +63,7 @@ export const getMembersCollection = (): Promise<MemberEntity[]> => {
6563

6664
> Using promises means you are going to use ES6 or you are going to include a
6765
> polyfill to provide compatibility on ES5 (need to install the polifyll and
68-
> include it your wepback entry point).
66+
> include it your webpack entry point).
6967
7068
- Let's jump into the ui side, to create the table component we are going to follow
7169
a progressive approach:
@@ -122,7 +120,7 @@ export const MemberTableComponent = () => {
122120
In this component we are doing many things:
123121

124122
- First we have created a custom hook to encapsulate the getter to memberCollection and
125-
the loadMemberCollection call to the api, by doing this code is more maintenalbe plus
123+
the loadMemberCollection call to the api, by doing this code is more maintenable plus
126124
is a potential effect to be reused in other components.
127125

128126
- We just use the custom hook that we have create in the component.
@@ -149,7 +147,7 @@ export * from "./sidebar";
149147
+ export * from './memberTable';
150148
```
151149

152-
- Let's instatiate this component into the app:
150+
- Let's instantiate this component into the app:
153151

154152
First we import it
155153

@@ -303,7 +301,7 @@ _./src/components/memberTable.tsx_
303301
```
304302

305303
> Excercise: we could go further with the refactoring, what about creating a
306-
> _TableHeaderComponent_ component and a _tableBodyComponent_ ?.
304+
> _TableHeaderComponent_ component and a _tableBodyComponent_ ?
307305
308306
# About Basefactor + Lemoncode
309307

hooks/11_TableAxios/Readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# 11 Table fetch
1+
# 11 Table Axios
22

3-
In this sample we are going to update the previous sampe (mock table) and instead of
3+
In this sample we are going to update the previous sample (mock table) and instead of
44
returning mock data, return real data from the github rest api.
55

66
# Steps to reproduce the sample
77

8-
- We will take as starting point sample _10 tableMock_, let's copy the content from this
8+
- We will take as starting point sample _10 TableMock_. Let's copy the content from this
99
sample and execute _npm install_.
1010

1111
```bash
1212
npm install
1313
```
1414

15-
- To retrieve data from the Github REST api we will make use of axios, let's install the package
15+
- To retrieve data from the Github REST api we will make use of axios. Let's install the package
1616
(no need to install typing, they are already included in the library).
1717

1818
```bash
@@ -68,7 +68,7 @@ export const getMembersCollection = (): Promise<MemberEntity[]> => {
6868
```
6969

7070
- Aaaand... we don't need to add any update on the rest of the application, why?
71-
The function is providing the same contract it returns a promise<MemberEntity[]>,
71+
The function is providing the same contract, it returns a promise<MemberEntity[]>,
7272
let's give a try:
7373

7474
```bash

hooks/12_ReactRouter/Readme.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 12 React Router
22

3-
n this sample we will start using React-Router (<acronym title="Single Page Application">SPA</acronym> navigation).
3+
In this sample we will start using React-Router (<acronym title="Single Page Application">SPA</acronym> navigation).
44

55
We take as a starting point the example _03 State_:
66

@@ -14,7 +14,7 @@ npm install
1414

1515
- Let's make some cleanup (remove _src/hello.tsx_ and _src/nameEdit.tsx_ files).
1616

17-
- Let's create a component called _PageA_ as _src/pageA.tsx_:
17+
- Let's create a component called _PageA_ as _src/pages/pageA.tsx_:
1818

1919
_./src/pages/pageA.tsx_
2020

@@ -27,7 +27,7 @@ export const PageA = () =>
2727
</div>
2828
```
2929

30-
- Let's create a component called _PageB_ as _src/pageB.tsx_:
30+
- Let's create a component called _PageB_ as _src/pages/pageB.tsx_:
3131

3232
_./src/pages/pageB.tsx_
3333

@@ -54,22 +54,29 @@ _./src/app.tsx_
5454
```diff
5555
import * as React from 'react';
5656
import * as ReactDOM from 'react-dom';
57-
- import { App } from './app';
5857
- import { HelloComponent } from './hello';
58+
- import { NameEditComponent } from './nameEdit';
5959
+ import { HashRouter, Switch, Route } from 'react-router-dom';
6060
+ import { PageA } from './pages/pageA';
6161
+ import { PageB } from './pages/pageB';
6262

63-
ReactDOM.render(
64-
- <HelloComponent userName={name} />
65-
- <NameEditComponent userName={name} onChange={setUsernameState} />
66-
+ <HashRouter>
67-
+ <Switch>
68-
+ <Route exact={true} path="/" component={PageA} />
69-
+ <Route path="/pageB" component={PageB} />
70-
+ </Switch>
71-
+ </HashRouter>,
72-
document.getElementById('root')
63+
export const App = () => {
64+
- const [name, setName] = React.useState("initialName");
65+
66+
- const setUsernameState = (event: React.ChangeEvent<HTMLInputElement>) => {
67+
- setName(event.target.value);
68+
- };
69+
return (
70+
<>
71+
- <HelloComponent userName={name} />
72+
- <NameEditComponent userName={name} onChange={setUsernameState} />
73+
+ <HashRouter>
74+
+ <Switch>
75+
+ <Route exact={true} path="/" component={PageA} />
76+
+ <Route path="/pageB" component={PageB} />
77+
+ </Switch>
78+
+ </HashRouter>,
79+
</>
7380
);
7481

7582
```

0 commit comments

Comments
 (0)