understanding redux — ilya gelman

45
UNDERSTANDING REDUX Ilya Gelman

Upload: 500tech

Post on 21-Jan-2017

514 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Understanding Redux — Ilya Gelman

UNDERSTANDING REDUX

Ilya Gelman

Page 2: Understanding Redux — Ilya Gelman

Ilya Gelman

- Organizer of AngularJS-IL & ReactJS-Israel - Passionate about design and UX

Consultant @ 500Tech

Page 3: Understanding Redux — Ilya Gelman

WHAT’S WRONG?

Page 4: Understanding Redux — Ilya Gelman

User Interface

Page 5: Understanding Redux — Ilya Gelman

User Interface

Controller

Page 6: Understanding Redux — Ilya Gelman

User Interface

Controller

Model Model

Page 7: Understanding Redux — Ilya Gelman

User Interface

Controller

Model Model Model

Page 8: Understanding Redux — Ilya Gelman

User Interface

Controller

Model Model Model

Model

Page 9: Understanding Redux — Ilya Gelman

User Interface

Controller

Model Model Model Model

Model

Page 10: Understanding Redux — Ilya Gelman

User Interface

Controller

Model

Library

Model Model Model

Model Model

Page 11: Understanding Redux — Ilya Gelman

User Interface

Controller

Model

LibraryLibrary

Model Model Model

Model Model Model Model

Page 12: Understanding Redux — Ilya Gelman

MVVC

Page 13: Understanding Redux — Ilya Gelman
Page 14: Understanding Redux — Ilya Gelman

NEW MINDSET

Page 15: Understanding Redux — Ilya Gelman

User Interface

Page 16: Understanding Redux — Ilya Gelman

User Interface

Dispatcher

Page 17: Understanding Redux — Ilya Gelman

User Interface

DispatcherStores

UNI-DIRECTIONAL DATA FLOW

StoresStores

Page 18: Understanding Redux — Ilya Gelman

UI IS RENDERED

Page 19: Understanding Redux — Ilya Gelman

UI IS RENDEREDSOMETHING HAPPENED

Page 20: Understanding Redux — Ilya Gelman

UI IS RENDEREDSOMETHING HAPPENEDSTATE CHANGEDUI IS RENDEREDSOMETHING HAPPENEDSTATE CHANGEDUI IS RENDEREDSOMETHING HAPPENEDSTATE CHANGEDUI RENDERED

Page 21: Understanding Redux — Ilya Gelman

FLUX

Page 22: Understanding Redux — Ilya Gelman

KEEP IT SIMPLE

Page 23: Understanding Redux — Ilya Gelman

Application state is…

ONE SINGLE PLAIN JAVASCRIPTOBJECT

Page 24: Understanding Redux — Ilya Gelman

Application state is…

READ-ONLY

Page 25: Understanding Redux — Ilya Gelman

PURE* FUNCTION

Current State

Action

New State

* No side-effects

How to change the state

Page 26: Understanding Redux — Ilya Gelman

PURE* FUNCTION

Current State

Action

New State

Reducer

Page 27: Understanding Redux — Ilya Gelman

PURE* FUNCTION

Current State

Action

New State

const newState = reducer(previousState, action);

{ type: 'ACTION_TYPE', /*...*/ }

Page 28: Understanding Redux — Ilya Gelman

import { createStore } from 'redux'; const store = createStore(reducer, initialState);

Responsible for creating a new state every time

The first state we pass to the reducer

Page 29: Understanding Redux — Ilya Gelman

Store

Page 30: Understanding Redux — Ilya Gelman

getState() dispatch()

subscribe()

Store

Page 31: Understanding Redux — Ilya Gelman

function createStore(reducer, initialState) { let state = initialState; return { dispatch(action) { state = reducer(state, action); }, getState() { return state; } }}

Basic Redux

Page 32: Understanding Redux — Ilya Gelman

REDUX ♥ VANILLA JS

Page 33: Understanding Redux — Ilya Gelman

REDUX

Page 34: Understanding Redux — Ilya Gelman

ONE MORE THING…

Page 35: Understanding Redux — Ilya Gelman

npm install --save react-redux

Page 36: Understanding Redux — Ilya Gelman

1. Put the in context

import { render } from 'react-dom'; import { Provider } from 'react-redux';

render ( <Provider store={ store }> <Root /> </Provider>, document.getElementById('app') );

Store

Page 37: Understanding Redux — Ilya Gelman

2. Connect components to store

import { connect } from 'react-redux'; const ImageList = ({ images }) => ( <ul> { images.map((url, index) => ( <li key={ index }>{ url }</li>) ) } </ul>); const mapStateToProps = (state) => ({ images: state.images });export default connect(mapStateToProps)(ImageList);

Page 38: Understanding Redux — Ilya Gelman

connect(mapStateToProps,mapDispatchToProps,…

)

Maps part of the stateto component’s props

Maps dispatch functionsto component’s props

Page 39: Understanding Redux — Ilya Gelman

REDUX ♥ REACT

Page 40: Understanding Redux — Ilya Gelman

REDUX ♥ ANGULAR

Page 41: Understanding Redux — Ilya Gelman

REDUX ♥ JQUERY

Page 42: Understanding Redux — Ilya Gelman

REDUX ♥ BACKBONE

Page 43: Understanding Redux — Ilya Gelman

♥ REDUX

Page 44: Understanding Redux — Ilya Gelman

QUESTIONS?

Page 45: Understanding Redux — Ilya Gelman

Read our blog:http://blog.500tech.com

Ilya [email protected]