ReactJS Intro
Andrew Dacenko <asd@fb.com>
Frontend Engineer @Facebook
August 6, 2018
Library for building Interfaces
Declarative
Component-Based
Write Everywhere
<Square color="black" />
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5156681/mobile-1976104_640.png)
<Form>
<Input />
<Input />
<FileInput />
<Submit />
</Form>
![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/512px-React-icon.svg.png)
Getting Started
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5156801/og_image.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5156803/node-js-736399_960_720.png)
Hello, World!
$ git checkout -b hello-world hello-world
$ yarn && yarn start
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5156874/Screenshot_2018-08-05_20.00.18.png)
Source Code
class HelloMessage extends React.Component {
render() {
return <div>{'Hello, ' + this.props.message + '!'}</div>;
}
}
ReactDOM.render(
<HelloMessage message="World"/>,
document.getElementById('root')
);
class HelloMessage extends React.Component {
render() {
return React.createElement(
'div',
null,
'Hello, ' + this.props.message + '!'
);
}
}
ReactDOM.render(
React.createElement(HelloMessage, { message: 'World' }),
document.getElementById('root')
);
Transformed Code
Task
Play around with props
- display your name
- display your age
- display your hometown
Data Flow
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5156976/flow-1024x693.png)
Component State
class Clicker extends React.Component {
state = {
count: 0,
};
render() {
return (
<div className="container">
<button className="button" onClick={() => {
this.setState({
count: this.state.count + 1,
});
}}>
Increment
</button>
<Counter count={this.state.count}/>
</div>
);
}
}
Task
- add Decrement button
- do not decrement below 0
- add Max count (9)
- display '9+' on Max
$ git checkout -b state-updates state-updates
State Management Libraries
Flux
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5157089/spaces--L5K1I1WsuQMZ8ecEuWg-avatar.png)
Redux
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5157096/mobx.png)
MobX
![](https://s3.amazonaws.com/media-p.slid.es/uploads/66414/images/5157101/logo.png)
Routing
import { BrowserRouter } from 'react-router-dom'
ReactDOM.render((
<BrowserRouter>
<App/>
</BrowserRouter>
), holder)
import { Route, Switch } from 'react-router-dom'
// when location = { pathname: '/about' }
<Route path='/about' component={About}/> // renders <About/>
<Route path='/contact' component={Contact}/> // renders null
<Route component={Always}/> // renders <Always/>
<Switch>
<Route exact path='/' component={Home}/>
<Route path='/about' component={About}/>
<Route path='/contact' component={Contact}/>
{/* when none of the above match, <NoMatch> will be rendered */}
<Route component={NoMatch}/>
</Switch>
Task
- add Short Description field
- add Description field using MD
- implement adding Products
- update cards to render Short Description
- add new Route to display Product
- implement "Add to Cart" / "Remove"
- show cart items count in header
- show cart items price in header
- persist data on changes
$ git checkout -b store store
Learn More
![](https://pbs.twimg.com/profile_images/889697663087542272/7mfszmjJ_400x400.jpg)
ReactJS Intro
By Andrew Dacenko
ReactJS Intro
ReactJS Intro Workshop for Coders of Colour Aug 6th, 2018
- 650