REACT IS A:
npm install react
▸ Let the fun begin!
UI LIBRARY
Slide 5
Slide 5 text
INSIDE A:
COMPONENT
Slide 6
Slide 6 text
UI ABSTRACTION:
COMPONENTS
▸ Exposes a React.Component
import React from ‘react’
Slide 7
Slide 7 text
COMPONENTS
OUTPUT: VIEW
▸ Matches the DOM
For your eyes only
Slide 8
Slide 8 text
REACT APPROACH
VIEW ONLY
▸ Just a view
▸ Just a library
▸ Bring your own: app framework, client router, http library,
module system, service layer, model library
REACT COMPONENTS ARE:
▸ Return DOM
function SideNav() {
return
...
}
JUST FUNCTIONS
Slide 11
Slide 11 text
REACT APPROACH
SEPARATION OF CONCERNS
▸ Not technology
▸ Uses JSX (superset of JS)
▸ Include what looks like HTML in your functions
function SideNav() {
return
...
}
Slide 12
Slide 12 text
COMPONENT STATE:
PROPS
▸ Immutable bag of data
▸ Flow into component from parent
function SideNav(props) {
return
{props.links}
}
▸ Allow dynamic output
▸ Like arguments to a function
Slide 13
Slide 13 text
JSX NOTE:
CURLIES
{props.links}
▸ Switch back to JS from HTML
▸ Inside curlies interpreted as {plain JS}
Slide 14
Slide 14 text
COMPONENT STATE:
PASS PROPS
▸ Like attributes in HTML
Slide 15
Slide 15 text
TWO STYLES:
COMPONENT SYNTAX
▸ Function style (aka “stateless components”)
import React from ‘react’
class MyComponent extends React.Component {}
▸ Class style
import React from ‘react’
function MyComponent() {}
STATE INITIALIZATION:
THIS.STATE
import React from ‘react’
class Counter extends React.Component {
constructor(props) {
super(props)
this.state = {
count: 0
}
}
}
Slide 18
Slide 18 text
STATE UPDATING:
THIS.SETSTATE
import React from ‘react’
class Counter extends React.Component {
handleCountUpdate() {
this.setState({ count: this.state.count + 1 })
}
}
▸ (the mutating)
Slide 19
Slide 19 text
COMPONENT:
RENDERING
Slide 20
Slide 20 text
COMPONENT CLASS:
RENDERING
import React from ‘react’
class SideNav extends React.Component {
render() {
return
{this.props.links}
}
}
▸ Note `this.props.links` vs. `props.links`
▸ Must implement `render`
Slide 21
Slide 21 text
COMPONENTS RENDER:
VIRTUAL DOM
import React from ‘react’
class SideNav extends React.Component {
render() {
return
{this.props.links}
}
}
▸ Return DOM… Virtual DOM
Slide 22
Slide 22 text
VIRTUAL DOM:
▸ In-memory representation
▸ Handled by React
▸ Manages changes, dirty checking
▸ Auto updates the real DOM
Real DOM
Virtual DOM React-managed
Browser-managed
Slide 23
Slide 23 text
JSX TRANSFORMED INTO:
FUNCTION CALLS
{props.links}
▸ Before
React.createElement(‘div’, props.links)
▸ After
REACT APPROACH
NO TEMPLATE LANGUAGE
▸ JSX small differences vs. HTML:
▸ camelCase attributes - e.g., `onClick` (except aria-* & html-*)
▸ html* prefix - e.g., `htmlFor`
▸ alternate words - e.g., `className`
▸ Curlies revert back to {JS}
Slide 26
Slide 26 text
CONNECT REACT TO:
REAL DOM
npm install react-dom
import { render } from ‘react-dom’
render(,
document.getElementById(‘app’))
▸ Call it once
▸ React handles translating VDOM to DOM
INSERTED INTO DOM:
“MOUNT”
▸ When your component is inserted into the real DOM
▸ 1. Created in memory
▸ 2. ReactDOM.render
▸ 3. Mount
▸ 4. Eventual removal called “unmount”
Slide 29
Slide 29 text
in DOM / visible
COMPONENT MOUNT:
Mount
Unmount
constructor
componentWillMount
componentDidMount
componentWillUnmount
▸
▸
▸
▸
▸
LIFECYCLE HOOKS
time
▸
Slide 30
Slide 30 text
ACCESS TO DOM:
REF
class UsernameField extends React.Component {
render() {
return this.input = el} />
}
}
Slide 31
Slide 31 text
ACCESS TO DOM:
DID MOUNT
class UsernameField extends React.Component {
componentDidMount() {
this.input.focus()
}
render() {
return this.input = i} />
}
}
EXTRACTING:
COMMONALITY
const UsernameField = props =>
const PasswordField = props =>
▸ Wrapped in two specific components:
Slide 64
Slide 64 text
REACT APPROACH
COMPOSITION
▸ Polymorphism via composition
▸ No inheritance
▸ Component wrapping is common
Slide 65
Slide 65 text
DOCUMENTING:
FOR THE FUTURE
npm install prop-types
▸ Discoverability, learning, correct usage
▸ Good citizen
▸ Describing the public interface (props)
Slide 66
Slide 66 text
DOCUMENTING:
FOR THE FUTURE
import PropTypes from ‘prop-types’
LoginField.propTypes = {
label: PropTypes.string,
name: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
type: PropTypes.string
}
▸ List all props
Slide 67
Slide 67 text
DOCUMENTING:
FOR THE FUTURE
▸ At dev time, if misuse prop, get warning
Warning: Failed prop type: The prop
`onChange` is marked as required in
`LoginField`, but its value is
`undefined`.
Slide 68
Slide 68 text
PROVIDING:
DEFAULTS
LoginField.defaultProps = {
type: ‘text’
}
▸ When specific props aren’t sent
▸ Use these values
CSS MODULES:
ISOLATED
▸ CSS exposed in export object
▸ Use original selector in code
import css from ‘./index.css’
Slide 79
Slide 79 text
CSS MODULES:
ISOLATED
▸ Value of selector name in export object is hashed
▸ Making them unique in global namespace
▸ Changes isolated to component
my-component.js
my-component.css
COMPONENT:
TESTING
npm install jest --save-dev
test(‘basic assertion’, () => {
expect(true).toEqual(true)
})
▸ Built on Jasmine
▸ Helpful with mocking
▸ Snapshot testing
Slide 82
Slide 82 text
SNAPSHOT:
TESTING
test(‘render correctly’, () => {
const tree = renderer.create()
expect(tree).toMatchSnapshot()
})
▸ Similar to taking a screenshot
▸ Comparing changes against original screenshot
▸ But “screenshot” is just a text serialization
STATE MANAGEMENT:
▸ Based on Flux pattern
▸ A pattern to help you organize app state
▸ Will complicate your app
▸ If benefit > complexity, do it
▸ Use `props` and `this.state` otherwise
REDUX
Slide 87
Slide 87 text
REACT PROJECT:
COMPLEXITY
React
Build toolchain
Large project
organization
Routing
Sharing
app state
▸ Around the edges
Data
Slide 88
Slide 88 text
REACT PROJECT:
COMPLEXITY
React
Routing Data
▸ Component-based
▸ Small to mid-sized
▸ = Joy
Slide 89
Slide 89 text
REACT IN THE:
LARGE
▸ Still the best
experience I’ve had
▸ Abstractions scale
well
▸ Redux implements a
solid pattern at scale
Slide 90
Slide 90 text
INTRODUCE REACT:
GRADUALLY
My Other-Framework app
ReactDOM.render(,
document.getElementById(‘small-piece’))
▸ Just need a DOM element to own
Slide 91
Slide 91 text
COMPONENTS ARE FOR:
SHARING
▸ Great encapsulation
▸ Simple public API
▸ Built-in documentation
▸ Productive community
npm publish
Slide 92
Slide 92 text
FIND SUPPORT:
IN THE COMMUNITY
▸ Questions:
▸ StackOverflow #reactjs
▸ Forum: discuss.reactjs.org
▸ Chat: “reactiflux” discord, #reactjs IRC
▸ OSS: github.com/reactjs
▸ Docs: facebook.github.io/react
Slide 93
Slide 93 text
FINISH:
YOUR TRAINING
▸ Pluralsight.com
▸ Egghead.io
▸ ReactTraining.com
▸ there.. is.. another..
Slide 94
Slide 94 text
A COUPLE:
EXAMPLE PROJECTS
github.com/jaketrent/wordspies
▸ React + Redux + more:
github.com/jaketrent/mastermind-saga
▸ Just React:
▸ (Disregard server portions)
Slide 95
Slide 95 text
USE:
WHAT YOU KNOW
▸ You know:
▸ How components work
▸ The React approach
▸ How to start a project
▸