Why I like React
About me
What is React?
Just the UI. It is not a competitor to Angular.
A better comparison is an Angular directive to React.
Built by Facebook + Instagram
Why use React?
React quick-start
Use a CDN or React’s Starter Kit: https://facebook.github.io/react/docs/getting-started.html
Hello World in React
Let us Code
helloworld.html
How does React work?
Does React have two-way binding?
STOP!
React will re-render your entire DOM on any state change. So, no two-way data binding.
Isn’t that slow?
Yes.
React uses a Virtual DOM (not to be confused with shadow DOM). React will build a DOM tree in memory and compare it to the actual DOM, build a diff, and make the least amount of changes.
Props
Treat these as immutable properties. If the props change; the component needs to re-render. Props do not mutate over time.
State
State can mutate over time. It is modified with this.setState.
React warns: “NEVER mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.”
Props vs State
Most of your components should have props. Try to keep as many of your components as possible stateless.
High level component should have state that flows down as props to lower components.
Component Methods
Lifecycle Methods
Events
Event delegation is built in.
Handlers can:
Let us Code
props.html
simplestate.html
state.html
React is functional
Pure functions should have an input that maps to exactly one output and have no observable side effects.
Let us Code
functional.html
Flux
Data comes in from one point.
Flux
There is no ‘true’ Flux library.
I have used quasi-Flux code, but have found a new library I like: https://github.com/gaearon/redux
Functional state.
Let us build something
flux.html