66
MANAGE THE FLUX OF YOUR WEB APPLICATION: LET’S REDUX

Manage the Flux of your Web Application: Let's Redux

Embed Size (px)

Citation preview

Page 1: Manage the Flux of your Web Application: Let's Redux

MANAGE THE FLUX OFYOUR WEB APPLICATION:

LET’S REDUX

Page 2: Manage the Flux of your Web Application: Let's Redux

Redux

Page 3: Manage the Flux of your Web Application: Let's Redux

Ego [email protected]

@TheStrazz86

https://github.com/francesco-strazzullo

https://medium.com/@TheStrazz86

https://slides.com/francescostrazzullo

Page 4: Manage the Flux of your Web Application: Let's Redux

http://codingjam.it/

Page 5: Manage the Flux of your Web Application: Let's Redux

https://taskomat.tech/it/

Page 6: Manage the Flux of your Web Application: Let's Redux

State Management

Page 7: Manage the Flux of your Web Application: Let's Redux

React

Page 8: Manage the Flux of your Web Application: Let's Redux

“ A JavaScript library for

building user interfaces

Page 9: Manage the Flux of your Web Application: Let's Redux
Page 10: Manage the Flux of your Web Application: Let's Redux

class HelloMessage extends React.Component {

render() {

return <div>Hello {this.props.name}!</div>;}

}

ReactDOM.render(<HelloMessage name="World" />, mountNode);

Page 11: Manage the Flux of your Web Application: Let's Redux

So Basically...it's not a

framework

Page 12: Manage the Flux of your Web Application: Let's Redux

Features

Page 13: Manage the Flux of your Web Application: Let's Redux

Components

Page 14: Manage the Flux of your Web Application: Let's Redux

JSX

Page 15: Manage the Flux of your Web Application: Let's Redux

class HelloMessage extends React.Component {

render() {

return React.createElement(

"div",

null,

"Hello ",

this.props.name

);}

}

ReactDOM.render(React.createElement(HelloMessage, { name: "World" }), mountNode);

Page 16: Manage the Flux of your Web Application: Let's Redux

Virtual Dom

Page 17: Manage the Flux of your Web Application: Let's Redux

State

Page 18: Manage the Flux of your Web Application: Let's Redux

class Timer extends React.Component {

constructor(props) {

super(props);this.state = {secondsElapsed: 0};

}

tick() {

this.setState((prevState) => ({

secondsElapsed: prevState.secondsElapsed + 1}));

}

componentDidMount() {

this.interval = setInterval(() => this.tick(), 1000);

}

componentWillUnmount() {

clearInterval(this.interval);

}

render() {

return (

<div>Seconds Elapsed: {this.state.secondsElapsed}</div>);

}

}

ReactDOM.render(<Timer />, mountNode);

Page 19: Manage the Flux of your Web Application: Let's Redux
Page 20: Manage the Flux of your Web Application: Let's Redux
Page 21: Manage the Flux of your Web Application: Let's Redux
Page 22: Manage the Flux of your Web Application: Let's Redux

Flux

Page 23: Manage the Flux of your Web Application: Let's Redux

“ Application Architecture For

Building User Interfaces

Page 24: Manage the Flux of your Web Application: Let's Redux

Why Not MVC?

Page 25: Manage the Flux of your Web Application: Let's Redux
Page 26: Manage the Flux of your Web Application: Let's Redux
Page 27: Manage the Flux of your Web Application: Let's Redux
Page 28: Manage the Flux of your Web Application: Let's Redux
Page 29: Manage the Flux of your Web Application: Let's Redux
Page 30: Manage the Flux of your Web Application: Let's Redux

Action: an event fired in the

system with the purpose to

change its state

Page 31: Manage the Flux of your Web Application: Let's Redux

Dispatcher: an event bus to

send actions into the system

Page 32: Manage the Flux of your Web Application: Let's Redux

Store: business logic objects.

Just like the M in MVC

Page 33: Manage the Flux of your Web Application: Let's Redux

View: React Components

Page 34: Manage the Flux of your Web Application: Let's Redux
Page 35: Manage the Flux of your Web Application: Let's Redux

Why Flux?

Page 36: Manage the Flux of your Web Application: Let's Redux

One Way of Thinking

Page 37: Manage the Flux of your Web Application: Let's Redux

Constant complexity on big

projects

Page 38: Manage the Flux of your Web Application: Let's Redux

Redux

Page 39: Manage the Flux of your Web Application: Let's Redux

“ Redux is a predictable state

container for JavaScript apps

Page 40: Manage the Flux of your Web Application: Let's Redux

Why not Flux?

Page 41: Manage the Flux of your Web Application: Let's Redux

One Way of Thinking (Really?)

Page 42: Manage the Flux of your Web Application: Let's Redux

Principles

Page 43: Manage the Flux of your Web Application: Let's Redux

"Single source of truth"

The state of your whole application is stored in

an object tree inside a single store.

Page 44: Manage the Flux of your Web Application: Let's Redux

"State is read-only"

The only way to mutate the state is to emit an

action, an object describing what happened.

Page 45: Manage the Flux of your Web Application: Let's Redux

"Mutations are written

as pure functions"

To specify how the state tree is transformed by

actions, you write pure reducers.

Page 46: Manage the Flux of your Web Application: Let's Redux

Elements

Page 47: Manage the Flux of your Web Application: Let's Redux

Actions

import Dispatcher from "src/Dispatcher";

var add = function(text) {

Dispatcher.dispatch({

actionType: "addTodo",

text: text

});};

var add = function(text) {

return {

actionType: "addTodo",

text: text

};};

Flux Redux

Page 48: Manage the Flux of your Web Application: Let's Redux

Reducer

function addTodo(state,text){

var toReturn = Object.assign({},state,{

todos:[...state.todos]

});

toReturn.todos.push(text);

return toReturn;

};

Page 49: Manage the Flux of your Web Application: Let's Redux

Store + Dispatcher

import { createStore } from 'redux';

import Reducers from 'src/Reducers';

let store = createStore(Reducers);

Page 50: Manage the Flux of your Web Application: Let's Redux

Container Components

Page 51: Manage the Flux of your Web Application: Let's Redux

Presentational Components

Page 52: Manage the Flux of your Web Application: Let's Redux

Why Redux?

Page 53: Manage the Flux of your Web Application: Let's Redux

One Way of Thinking (Really

this time)

Page 54: Manage the Flux of your Web Application: Let's Redux

Serilizable State

Page 55: Manage the Flux of your Web Application: Let's Redux

No side-effect

Page 56: Manage the Flux of your Web Application: Let's Redux

No Technical Debt

Page 57: Manage the Flux of your Web Application: Let's Redux
Page 58: Manage the Flux of your Web Application: Let's Redux

Async Actions

Page 59: Manage the Flux of your Web Application: Let's Redux

onClick() {

this.props.dispatch(actions.requestData())

this.api.list().then(response => {

this.props.dispatch(actions.requestDataSuccess(response.data))

}).catch(error => {

this.props.dispatch(actions.requestDataError(error))

})}

Page 60: Manage the Flux of your Web Application: Let's Redux

redux-saga

Page 61: Manage the Flux of your Web Application: Let's Redux

“ A Saga is an independent

component that reacts to

domain events in a cross-

aggregate, eventually

consistent manner.

Page 62: Manage the Flux of your Web Application: Let's Redux

“ redux-saga is a library that

aims to make side effects in

Redux applications easier and

better.

Page 63: Manage the Flux of your Web Application: Let's Redux

function* fetch() {

try {

const data = yield call(api.list);yield put(actions.requestDataSuccess(data));

} catch (e) {

yield put(actions.requestDataError(e));

}

}

function* mySaga() {

yield takeLatest("REQUEST_DATA", fetch);

}

export default mySaga;

Page 64: Manage the Flux of your Web Application: Let's Redux
Page 65: Manage the Flux of your Web Application: Let's Redux

That's It!

[email protected]

@TheStrazz86

https://github.com/e-xtrategy/javascript-course/

Page 66: Manage the Flux of your Web Application: Let's Redux

Thanks!