Mock app
What is React?
Design patterns
Assessment criteria
> git clone https://github.com/sarahdherr/dotreact
> cd DotReact
> npm install
> npm run build
> npm start
React is a JavaScript library created by Facebook in 2011 and first deployed in 2013 at JSConf
It is used by many large tech companies, including Netflix, Paypal, AirBnB and Dropbox
Note: React is not a full framework. It corresponds to the View in the Model-View-Controller (MVC) pattern.
Writing cross platform applications is easier with React because you don't have to learn a new framework
Many companies adopted React due to mobile first design
It is not fully "write once, run anywhere", but once you learned React writing different implementations is not bad
React favors composition over inheritance.
Making contained and specialized components over extending an existing component.
Dialog(props) =>
<FancyBorder color="blue">
<h1 className="dialog-title>
{props.title}
</h1>
<p className="dialog-msg">
{props.message}
</p>
</FancyBorder>
WelcomeDialog(props) =>
<Dialog
title="Welcome"
message="We have cookies"
/>
Components are encapsulated to manage their own state
Since the UI logic is separated from the JSX, you can pass rich data to child components while keeping state out of the DOM
React creates an in-memory data structure cache and computes the diff for re-rendering
It updates the browsers DOM efficiently
React only re-renders sub components that actually changed
React has data flow down to children components through props - ideally immutably
Properties flow down, actions flow up
class ListContainer extends React.Component {
constructor(props : ListContainerProps) {
super(props);
this.state = {
items: []
}
}
componentDidMount() {
axios.get(`url.com/{this.props.apiCall}`)
.then((items : Array<any>) =>
this.setState({ items }))
}
render() {
return (
<List items={this.state.items} />
)
}
}
const List = (props : ListProps) =>
<div>
{
props.items.map(el =>
<ListItem item={el} />)
}
</div>
const ListItem = (props : ListItemProps) =>
<div>
<h1>{ item.name }</h1>
<p>{ item.companyName }</p>
<Score score={ item.score } />
</div>
Because it is only defining the "view" of the application, it doesn't care where the data is coming from or going to
It is fully JavaScript, so anything that interacts with other JS libraries can interact easily with React
React is not very opinionated. So you can take what you want and leave the rest
console.log()
How easy is it to add functionality?
Once you set it up, adding functionality is quick and easy
How difficult is it to add bad code?
It is just as easy to write bad code as good code
How easy is it to read the code?
React is easy to read once you know it
How difficult is it to break something unintentionally?
Typescript helps decrease accidentally breaking something
A library that allows you to use a type system in JavaScript
// Vanilla JS
function includes(name, names) {
let index = names.indexOf(name);
return index != -1;
}
// Typescript
function includes(name : string, names : Array<string>) : boolean {
let index : number = names.indexOf(name);
return number != -1;
}
Renders pretty quickly
However, there is some criticism of React due to high memory requirements from the virtual DOM
Lines of code: 475
The React community is huge. The github repo has been starred 92,695 times and is backed by Facebook
Any JS library integrates well with React. And since it is so popular, many have made React versions of popular libraries
The learning curve can be steeper because you need to understand all the nuances of JS (ie. this) as well
There are many testing frameworks that work well with React, specifically Enzyme (made by AirBnB) which have implementations with Jest, Mocha and many more