Реактивная
тяга
Ярослав Савченко
EPAM
Problem
Complicated and unpredictable state
MVC
Real MVC
UI = f(state)
Flux
Flux
React
“ We built React to solve one problem: building
large applications with data that changes over
time. “
React documentation
Virtual DOM
React - Hello world
class HelloMessage extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>Hello {this.props.name}</div>;
}
}
React - Hello world
class HelloMessage extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>Hello {this.props.name}</div>;
}
}
React - JSX
render() {
return <div>Hello {this.props.name}</div>;
}
render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
“ We think that markup and the code that
generates it are intimately tied together. ”
React documentation
React - component creation
class HelloMessage extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>Hello {this.props.name}</div>;
}
}
ReactDOM.render(<HelloMessage name="World" />, mountNode);
React - props
class HelloMessage extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div onClick={this.props.handleClick}>
Hello {this.props.name}
</div>;
}
}
ReactDOM.render(<HelloMessage
name="World"
handleClick={someCallback} />, mountNode);
React - props validation
class HelloMessage extends React.Component {
propTypes: {
name: React.PropTypes.string,
name: React.PropTypes.oneOf(["World", "People"])
},
constructor(props) { /* … */ }
render() {
return <div>Hello {this.props.name}</div>;
}
}
React - state
class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = { name: this.props.name || "World" };
}
handleClick = (event) => {
this.setState({ name: event.target.value });
}
render() {
return (<div>Hello
<input
value={this.state.name}
onChange={this.handleClick}/>
</div>);
}
}
React - stateless function
const HelloMessage = (props) => {
let name = props.name.toUpperCase();
return <div>Hello {this.props.name}</div>
}
/* or */
const HelloMessage = ({name}) => <div>Hello {name}</div>;
Component Lifecycle
Mount
Update
Unmount
...
Update
Component Lifecycle
Mount
- getDefaultProps
- getInitialState
- componentWillMount
- render
- componentDidMount
Update
- componentWillReceiveProps
- shouldComponentUpdate
- componentWillUpdate
- render
- componentDidUpdate
Unmount
- componentWillUnmount
Redux
Redux
… is a predictable state container for JavaScript apps.
Three Principles:
● Single source of truth
● State is read-only
● Changes are made with pure functions
Redux
Single source of truth
The state of your whole application is stored in an object tree within a single store
let initialState = {
todos: [ {...}, {...}, … {...} ],
filter: "active"
}
Redux
State is read-only
The only way to mutate the state is to emit an action, an object describing what
happened
const action = {
type: SET_VISIBILITY_FILTER,
filter: "active"
}
Redux
Changes are made with pure functions
Pure reducers specify the state tree transformations by actions
const todoApp = (state = initialState, action) => {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return {...state, visibilityFilter: action.filter};
default:
return state;
}
}
Redux
The Store is the object that brings them together
Create store:
let store = createStore(todoApp, initialState);
Dispatch an action:
store.dispatch(filterAction);
Subscribe/unsubscribe
let unsubscribe = store.subscribe(() =>
console.log(store.getState());
);
Why React and Redux?
Because now UI=React(Redux)
Advantages of these approach
● Simplify app
● Debugging
● Testing
● Performance
● Reusability
● Bleeding-edge features
(Server-side render, CSS Modules etc.)
Problems
● Javascript Fatigue
● Build (React, Redux, react-redux,
react-router, ES2015, Babel,
webpack)
● Only View and Store
● Inconsistent React docs
● Big size (React ~26kb,
Backbone ~20kb, Angular ~40kb)
Holywar?
Links to start with
● Pete Hunt: React How-to
https://github.com/petehunt/react-howto
● Getting started with React
http://facebook.github.io/react/docs/getting-started.html
● Dan Abramov: Getting started with Redux
https://egghead.io/series/getting-started-with-redux
● 0 to 1 : Getting started with Redux
http://www.jchapron.com/2015/08/14/getting-started-with-redux/
https://habrahabr.ru/post/269831/
Thanks,
and have a good flight!

Reactивная тяга

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    “ We builtReact to solve one problem: building large applications with data that changes over time. “ React documentation
  • 11.
  • 12.
    React - Helloworld class HelloMessage extends React.Component { constructor(props) { super(props); } render() { return <div>Hello {this.props.name}</div>; } }
  • 13.
    React - Helloworld class HelloMessage extends React.Component { constructor(props) { super(props); } render() { return <div>Hello {this.props.name}</div>; } }
  • 14.
    React - JSX render(){ return <div>Hello {this.props.name}</div>; } render() { return React.createElement("div", null, "Hello ", this.props.name); }
  • 15.
    “ We thinkthat markup and the code that generates it are intimately tied together. ” React documentation
  • 16.
    React - componentcreation class HelloMessage extends React.Component { constructor(props) { super(props); } render() { return <div>Hello {this.props.name}</div>; } } ReactDOM.render(<HelloMessage name="World" />, mountNode);
  • 17.
    React - props classHelloMessage extends React.Component { constructor(props) { super(props); } render() { return <div onClick={this.props.handleClick}> Hello {this.props.name} </div>; } } ReactDOM.render(<HelloMessage name="World" handleClick={someCallback} />, mountNode);
  • 18.
    React - propsvalidation class HelloMessage extends React.Component { propTypes: { name: React.PropTypes.string, name: React.PropTypes.oneOf(["World", "People"]) }, constructor(props) { /* … */ } render() { return <div>Hello {this.props.name}</div>; } }
  • 19.
    React - state classHelloMessage extends React.Component { constructor(props) { super(props); this.state = { name: this.props.name || "World" }; } handleClick = (event) => { this.setState({ name: event.target.value }); } render() { return (<div>Hello <input value={this.state.name} onChange={this.handleClick}/> </div>); } }
  • 20.
    React - statelessfunction const HelloMessage = (props) => { let name = props.name.toUpperCase(); return <div>Hello {this.props.name}</div> } /* or */ const HelloMessage = ({name}) => <div>Hello {name}</div>;
  • 21.
  • 22.
    Component Lifecycle Mount - getDefaultProps -getInitialState - componentWillMount - render - componentDidMount Update - componentWillReceiveProps - shouldComponentUpdate - componentWillUpdate - render - componentDidUpdate Unmount - componentWillUnmount
  • 23.
  • 24.
    Redux … is apredictable state container for JavaScript apps. Three Principles: ● Single source of truth ● State is read-only ● Changes are made with pure functions
  • 25.
    Redux Single source oftruth The state of your whole application is stored in an object tree within a single store let initialState = { todos: [ {...}, {...}, … {...} ], filter: "active" }
  • 26.
    Redux State is read-only Theonly way to mutate the state is to emit an action, an object describing what happened const action = { type: SET_VISIBILITY_FILTER, filter: "active" }
  • 27.
    Redux Changes are madewith pure functions Pure reducers specify the state tree transformations by actions const todoApp = (state = initialState, action) => { switch (action.type) { case SET_VISIBILITY_FILTER: return {...state, visibilityFilter: action.filter}; default: return state; } }
  • 28.
    Redux The Store isthe object that brings them together Create store: let store = createStore(todoApp, initialState); Dispatch an action: store.dispatch(filterAction); Subscribe/unsubscribe let unsubscribe = store.subscribe(() => console.log(store.getState()); );
  • 29.
    Why React andRedux? Because now UI=React(Redux)
  • 30.
    Advantages of theseapproach ● Simplify app ● Debugging ● Testing ● Performance ● Reusability ● Bleeding-edge features (Server-side render, CSS Modules etc.)
  • 31.
    Problems ● Javascript Fatigue ●Build (React, Redux, react-redux, react-router, ES2015, Babel, webpack) ● Only View and Store ● Inconsistent React docs ● Big size (React ~26kb, Backbone ~20kb, Angular ~40kb)
  • 32.
  • 33.
    Links to startwith ● Pete Hunt: React How-to https://github.com/petehunt/react-howto ● Getting started with React http://facebook.github.io/react/docs/getting-started.html ● Dan Abramov: Getting started with Redux https://egghead.io/series/getting-started-with-redux ● 0 to 1 : Getting started with Redux http://www.jchapron.com/2015/08/14/getting-started-with-redux/ https://habrahabr.ru/post/269831/
  • 34.
    Thanks, and have agood flight!