using redux with react

10
REDUX BY DAN JIPA, TEAM LEADER SOFTVISION a predictable state container for JavaScript apps

Upload: dan-jipa

Post on 09-Jan-2017

110 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Using Redux with React

REDUX

BY DAN JIPA, TEAM LEADER SOFTVISION

a predictable state container for JavaScript apps

Page 2: Using Redux with React

The need

Managing state in big applications is too complicated.

REDUX By Dan Jipa [[email protected]]

There is no separation of concerns.Simplified debugging.! Single Source of Truth

Page 3: Using Redux with React

Traditional communication between components

By Dan Jipa [[email protected]]REDUX

1. Props

2. Callback functions

3. Event bubbling

4. Parent component

5. Observer pattern (publish,subscribe)

6. Global vars

1 2, 3 4 5, 6, 7

7. Context

Image: http://andrewhfarmer.com/component-communication/

Page 4: Using Redux with React

Traditional data fetching

By Dan Jipa [[email protected]]REDUX

1. Root component

2. Container component

Page 5: Using Redux with React

By Dan Jipa [[email protected]]

What do we need to start using Redux with React?

REDUX

Webpack (optional)

Babel (optional)

Redux

React - Redux

Middleware (Redux Thunk)

Request library (Fetch, Axios etc)

Page 6: Using Redux with React

By Dan Jipa [[email protected]]

Redux in a few words

REDUX

Store = {global application state}; dispatch()

Actions <User, Browser, Server> = the only way to change state

Reducers = mapping actions in state updates

connect() = sending data to low level components via props

high level/container components = components with access to global store

Page 7: Using Redux with React

By Dan Jipa [[email protected]]

Updating our state

REDUX

CURRENT STATEAction

REDUCER NEW STATE

Page 8: Using Redux with React

By Dan Jipa [[email protected]]

Fetching data with Redux

REDUX

import { createStore, applyMiddleware } from 'redux'import thunk from 'redux-thunk'; import rootReducer from '../reducers'

export default function configureStore(initialState) { const store = createStore(rootReducer, initialState, applyMiddleware(thunk)); return store}

Creating our initial store

Modifying it using actionsexport function translate(text, target) { return dispatch => { return fetch('https://www.googleapis.com/language/translate/v2?key='+API_KEY+'&source=en&target='+target+'&q='+ text) .then(response => response.json()) .then(response => dispatch(receiveTranslation(text, target, response.data.translations[0].translatedText))) }}

Page 9: Using Redux with React

By Dan Jipa [[email protected]]

LEARN REDUX

REDUX

How to manage request state with React & Redux

Redux - official docs

A cartoon intro to Redux

Getting started with Redux - Video lectures by Dan Abramov

React - Redux - Udemy course (PAID)

Page 10: Using Redux with React

By Dan Jipa [[email protected]]

fb: dan.jipa, tw: danjipa, [email protected]

Thank you!

Let’s stay in touch

REDUX