Top Banner
React A web tool developed and open-sourced by Facebook
37
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Understanding Facebook's React.js

ReactA web tool developed and open-sourced by Facebook

Page 2: Understanding Facebook's React.js

• React is a javascript library, or tool

• framework agnostic

• lightweight and recursive

What is React?

Page 3: Understanding Facebook's React.js

What is React?

components

that

re-render

Page 4: Understanding Facebook's React.js

Benefits of React

• Very modular .:. reusable

• State-based .:. predictable

• Independent .:. testable

• High-performance, thanks to virtual DOM

Listing Card

Photo Gallery

Page 5: Understanding Facebook's React.js

React Virtual DOMthe ability to re-render on every change!

Page 6: Understanding Facebook's React.js

• Manipulating the DOM is slow.

• Javascript is fast.

React Virtual DOM

Page 7: Understanding Facebook's React.js

Browser

React Virtual DOM

User’s View Real DOM

Background Javascript Process Virtual DOM

Page 8: Understanding Facebook's React.js

Browser

React Virtual DOM

User’s View Real DOM

null

Background Javascript Process Virtual DOM

<html> <button> Add text</button>

<p></p> </html>

First Render

Page 9: Understanding Facebook's React.js

Browser

React Virtual DOM

User’s View Real DOM

<html> <button> Add text</button>

<p></p> </html>

Background Javascript Process Virtual DOM

Real DOM populated, vDOM cleared

Page 10: Understanding Facebook's React.js

Browser

React Virtual DOM

User’s View Real DOM

<html> <button> Add text</button>

<p></p> </html>

Background Javascript Process Virtual DOM

<html> <button> Add text</button>

<p>New text!</p> </html>

User clicks ‘Add text’!

Page 11: Understanding Facebook's React.js

Browser

React Virtual DOM

User’s View Real DOM

<html> <button> Add text</button>

<p>New text!</p> </html>

Background Javascript Process Virtual DOM

<html> <button> Add text</button>

<p>New text!</p> </html>

User clicks ‘Add text’!

Page 12: Understanding Facebook's React.js

• What we get is a seamless, fluid ui

• Only things that actually change, do change

React Virtual DOM

Page 13: Understanding Facebook's React.js

• No need to implement the vDOM

• But it’s important to know the foundation of the tools you choose!

React Virtual DOM

Page 14: Understanding Facebook's React.js

JSX is a sugar-syntax for JS

React JSX

Page 15: Understanding Facebook's React.js

React JSX

=

JSX

JS

Page 16: Understanding Facebook's React.js

React JSXJSX can be escaped from JSX-land to JS-land with {}

JSX

JS

Page 17: Understanding Facebook's React.js

React JSXThat goes for comments too!

JSX

JS

Page 18: Understanding Facebook's React.js

React JSXIt’s javascript! Use expressions.

JSX

JS

Page 19: Understanding Facebook's React.js

React JSX

Hmm….

Why not just write javascript… ?

Page 20: Understanding Facebook's React.js

React JS

Page 21: Understanding Facebook's React.js

React JS

Page 22: Understanding Facebook's React.js

React JSX

Page 23: Understanding Facebook's React.js

component = view + controller

React Components

Page 24: Understanding Facebook's React.js

• Forget MVC, think components.

• Component = view/html + controller/js

• JSX (sugar syntax that gets compiled to JS)

React Components

Page 25: Understanding Facebook's React.js

React Components

Page 26: Understanding Facebook's React.js

AppTemplate

UserDashboardPage

Listing Card

Photo Gallery

Listing Card

Photo Gallery

Listing Card

Photo Gallery

Listing Card

Photo Gallery

Listing Card

Photo Gallery

Listing Card

Photo Gallery

HeaderNav

React Components

Page 27: Understanding Facebook's React.js

• Based on state (props and state)

• Uni-directional (re-renders every change*)

• Composable (components in components)

• Reusable (thanks to props)

React Components

Page 28: Understanding Facebook's React.js

React Components

Page 29: Understanding Facebook's React.js

Component Lifecycle• componentWillMount()

• componentDidMount() *client only

• componentWillReceiveProps()

• shouldComponentUpdate()

• componentWillUpdate()

• componentDidUpdate()

• componentWillUnmount()

Page 30: Understanding Facebook's React.js

• React allows you to build react classes of components

• From those classes, you build component instances, or elements

• Factories create functions that return elements for a given class

React.createClass()

React.createElement()

React.createFactory()

React Components

Page 31: Understanding Facebook's React.js

Performance Components

• High-performance, thanks to virtual DOM

• Automatically applies best practices

• UI handlers, batch manipulations

Page 32: Understanding Facebook's React.js

Summarizing Components

• Very modular .:. reusable

• State based .:. predictable

• Independent .:. testable

• High-performance, thanks to virtual DOM

Listing Card

Photo Gallery

Page 33: Understanding Facebook's React.js

in React Components

State vs Props

Page 34: Understanding Facebook's React.js

React Components

Page 35: Understanding Facebook's React.js

Component Data

• State — contained within a component; used to track changes within a component

• Props — passed in from parent; think of these as arguments, or inputs to a component; do not change

Page 36: Understanding Facebook's React.js

Component State

• Things that change within the component

• Think of an input form, when the user types stuff —> Keep track of those changes in state

• … or the currentPhotoIndex the user is on as he swipes through the PhotoGallery —> Keep track of those changes in state

Photo Gallery1 of 12

Page 37: Understanding Facebook's React.js

Component Props

• Things that get passed into the component

• Think of props as configurations to your component <PhotoGallery photoCount=“false” /> <PhotoGallery photoCount=“true” />

• The parent component passes down data into the child component via props.

Photo Gallery1 of 12

Photo Gallery