Linus Falck-Ytter
Vermont Code Camp
Software Developer @ Liberty Mutual
@lifayt
github.com/lifayt
lifayt@gmail.com
Thanks to our Sponsors!
In the beginning, there was nothing.
In the beginning, there was nothing.*
*Except a lot of ways to make websites that generally worked pretty good, most of the time.
Programmers said, 'Let there be JavaScript Libraries!'
And there were JavaScript Libraries. There was still nothing* but you could write a whole lot more code.
*Except a lot of ways to make websites that generally worked pretty good, most of the time.
ReactJS
Latest: v16.5.0
Initial Release: 3/2015
Rationalization, History, and Benefits:
import/export
class (super, class-field)
arrow functions ( () => {} )
React is it's own separate skill from setting up react
Use create-react-app
Everyone benefits from learning webpack/parcel
Familiarize yourself with the wider JS ecosystem
Don't sweat the weird stuff
A paradigm:
A different paradigm:
Why do JS 'frameworks' exist?
Like most JS frameworks/libraries, React is built around the idea of reusable components.
All components are similar to JavaScript functions (although they can be both JavaScript functions and JavaScript classes)
Components represent independent, reusable slices of your UI and application
React embraces the fact that rendering logic is inherently coupled with other UI
logic: how events are handled, how the state changes over time, and how the data
is prepared for display.
Instead of artificially separating technologies by putting markup and logic in
separate files, React separates concerns with loosely coupled units called
“components” that contain both.
https://reactjs.org/docs/introducing-jsx.html
https://medium.com/@baphemot/understanding-react-react-16-3-component-life-cycle-23129bc7a705
constructor
Default State, maybe some calculation to initialize state
static getDerivedStateFromProps
Derive state from props. rare.
render
Initial render of component. Remember to have default cases set up if you're waiting for data!
componentDidMount
Side effects: Fetch data, interact with DOM that was rendered, etc.
static getDerivedStateFromProps
Derive state from props. rare.
shouldComponentUpdate
Performance optimization - should I re-render?
render
Batched for optimization
getSnapshotBeforeUpdate
Capture some information about the DOM (scroll position, screen size). Useful for animations.
componentDidUpdate
Operate on DOM, fetch data, set state. rare.
componentWillUnmount
Clean up DOM, component. rare.
componentWillMount
Prep stuff for mounting - use constructor.
componentWillUpdate
Just avoid.
componentWillReceiveProps
Derive new state from props - use a combination of getDerivedStateFromProps and componentDidUpdate.
These methods are DEPRECATED and marked as UNSAFE__ - avoid them in new code, refactor in old code.
Thinking in React