+ All Categories
Home > Documents > MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application...

MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application...

Date post: 03-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
101
4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MANAGE STATE MANAGE STATE OF OF A LARGE REACT APP A LARGE REACT APP www.thatJSDude.com
Transcript
Page 1: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101

MANAGE STATEMANAGE STATEOFOF

A LARGE REACT APPA LARGE REACT APPwww.thatJSDude.com

Page 2: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 2/101

WE WILL COVERWE WILL COVER1. What is State2. 5 scenarios to Manage state3. Redux4. Redux-saga

Page 3: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 3/101

WHAT IS A STATE?WHAT IS A STATE?

Page 4: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 4/101

THINK FOR A SECOND...THINK FOR A SECOND...

It's summer timeYou are sitting in the beachUnder a large beach umbrellaAnd your friend is grilling some steak

Your state is: pretty RELAXED

Page 5: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 5/101

THINK FOR ANOTHER SECOND...THINK FOR ANOTHER SECOND...

You are in a big ConferenceTalking in front of 30+ peopleTrying to explain what is stateEveryone is staring at you

Your state is: pretty NERVOUS

This means state of your mind changes based on the situation

Page 6: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 6/101

SO WHAT IS A STATE?SO WHAT IS A STATE?State is something

That might changeLike: your mood, your confidence levelOr London Weather: winter, rain, 2 weeks ofsummer!

Page 7: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 7/101

IN A REACT APPIN A REACT APP

You can think of two level of states

1. Application State: Global StateLike: Session storage/ Database/othersSimply think it like a databaseAny component, anywhere in the app canaccess

2. Component State

Within that specific componentCan only be updated within that componentCan be passed down to its children via props

React States

Page 8: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 8/101

THINK ABOUT AN APPLICATIONTHINK ABOUT AN APPLICATIONEMA­JOHNEMA­JOHN

Page 9: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 9/101

Your manager tells you to

ADD MORE FEATURES...ADD MORE FEATURES...

Page 10: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 10/101

MORE FEATURES MEAN...MORE FEATURES MEAN...

More componentsMore data, more props has to pass dataMore event handler has to be managedState grows and becomes complicated And you create more mess...

But Don't worry: the bigger is the mess, this higher is the jobsecurity

Page 11: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 11/101

REDUXREDUXWill start knocking at your door

Page 12: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 12/101

REDUX WILL ASK YOUREDUX WILL ASK YOUPut app state out of your componentCreate state as plain objects and arraysDescribe changes as plain objectsHandling changes as pure functions

None of these is a must to build an app

Page 13: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 13/101

ADDING ADDING REDUXREDUXWill add an indirection to your problemWill shift the complexity in a different directionCould make your situation WORSE

Is redux a must?

Page 14: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 14/101

ASSES YOUR APPLICATIONASSES YOUR APPLICATION

How many sibling Components have tocommunicate?Does child component contain state from parentcomponent?How many different views need to share state?

Page 15: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 15/101

ASSESS YOUR TEAMASSESS YOUR TEAMDo a proof of concept in a feature branchExplore how it solves the problem before divinginto itDoes team understand: Redux core concepts.Can handle extra level of complexityImmutabilty, pure function, etc.

Page 16: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 16/101

USE CASE­1USE CASE­1

You are building from scratch

Page 17: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 17/101

START WITH A MOCKUPSTART WITH A MOCKUP

[ {price: "$49.99", stocked: true, name: "Football"}, {price: "$9.99", stocked: true, name: "Baseball"}, {price: "$29.99", stocked: false, name: "Basketball"}, {price: "$99.99", stocked: true, name: "iPod Touch"}, {price: "$399.99", stocked: false, name: "iPhone 5"}, {price: "$199.99", stocked: true, name: "Nexus 7"}, ];

Think in React

Page 18: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 18/101

YOU STARTED LOADING DATA FROMYOU STARTED LOADING DATA FROMTHE APITHE API

Adding two lists in the state

componentWillMount() { Promise.all([ api.shop.getProducts(), api.shop.getCurrentOrder() ]).then(([products, order]) => { // Extract ids of the products in my order now const orderProductIds = order.map(p => p.id); // Set "selected" flag on all products const uiProducts = products.map(p => { const selected = orderProductIds.indexOf(p.id) !== -1; return { ...p, selected }; }); this.setState({ products: uiProducts, order }); }); }

Page 19: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 19/101

Page 20: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 20/101

HANDLE A PRODUCT TOGGLEHANDLE A PRODUCT TOGGLE

toggleProductInOrder(product) { // Take all products, flip selection of the toggled one const newProducts = this.state.products.map(p => { if (p.id !== product.id) { return p; } return { ...p, selected: !p.selected }; }); // Filter out the selected products const newOrderProducts = newProducts.filter(p => p.selected) // Update both the order and the products return api.shop.updateOrder(newOrderProducts) .then(newOrder => { this.setState({ order: newOrder, products: newProducts } }); }

Page 21: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 21/101

CAN WE MAKE STATE SIMPLER?CAN WE MAKE STATE SIMPLER?

Try to Avoid changing data you get from the APIHandle Derived Property in RenderTry to avoid 2 or more state change with one event

componentWillMount() { Promise.all([ api.shop.getProducts(), api.shop.getCurrentOrder() ]).then(([products, order]) => { // No pre-processing of our data from the server this.setState({ products, order }); }); } getSelected(product) { return this.state.order.find(p => p.id === product.id); }

Page 22: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 22/101

If possible calculate derived properties of your data duringrender()

Page 23: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 23/101

toggleProductInOrder(product) { const order = this.state.order; const selectedProduct = this.getSelected(product); //Updating an order is just adding or removing on item const updatedOrder = selectedProduct ? without(order, product) : concat(order, product) ; return api.shop.updateOrder(updatedOrder) .then(newOrder => { this.setState({ order: newOrder }); //update one state }); }

Manage State in React

Page 24: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 24/101

DIGESTIBLE EXAMPLEDIGESTIBLE EXAMPLECompute From states

class MyComponent extends Component { constructor(props) { super(props); this.state = { message: props.message + "!" }; } componentWillReceiveProps(props) { this.setState({ message: props.message + "!" }); } render() { return ( <div>{this.state.message}</div> ); } }

Page 25: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 25/101

Just read from the props/state

Avoid using state for data which can be calculated from props orother state

render() { const excitedMessage = this.props.message + "!"; return (<div>{excitedMessage}</div>); }

Page 26: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 26/101

WHAT IF YOU CANWHAT IF YOU CAN

RECREATE THE WHEELRECREATE THE WHEEL

Page 27: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 27/101

IDENTIFY COMPONENT HIERARCHYIDENTIFY COMPONENT HIERARCHY

Draw boxes around componentUse Single responsibility principleOne component should ideally do one thing

Page 28: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 28/101

NAME YOUR COMPONENTSNAME YOUR COMPONENTS1. ProductRow (red)2. ProductContainer (green)3. SearchBar (blue)4. FilterableProductContainer (orange)

Page 29: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 29/101

DATA YOU MIGHT HAVE...DATA YOU MIGHT HAVE...During Run time

List of productsOrder listSearch text the user hasenteredValue of the checkboxFiltered list of products

Page 30: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 30/101

IDENTIFY UI STATEIDENTIFY UI STATE

It's not a React state if...

1. Just passed from parent via props2. Remain unchanged over time3. Can be computed by using other state/props

Page 31: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 31/101

WHERE TO PUT THE STATEWHERE TO PUT THE STATE1. Identify component that just has to render2. Put state in the common owner in the higher up3. If you don't find a common owner, create one

(Apply these 3 rules)

ProductContainer doesn't have filters informationSearchBar doesn't have product listFilterableProductList is the common ownerFilterableProductList will contain the state

working code

Page 32: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 32/101

STATESTATE ME IF YOU CAN... ME IF YOU CAN...

Products: state (could be a property)Filtered product can be computed via search textand checkbox: not stateIs item selected: not stateOrder list changes on toggle: stateSearch text user can entered: stateValue in the checkbox: state

Page 33: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 33/101

SUMMARYSUMMARYTry not to change data you get from serverAvoid storing data in this.state which can becalculated from props/stateTry to put derived property in render()Try to avoid single event changing multiple state

Handle state in React

Page 34: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 34/101

USE CASE ­ 2USE CASE ­ 2

YOU HAVE A LITTLE MESSYOU HAVE A LITTLE MESSWhen you need to Share state

Page 35: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 35/101

HIGHER ORDER COMPONENTHIGHER ORDER COMPONENT1. Divide components into containers and presenters2. Pass callbacks to hand as state change in children3. State sharing components in container

Lifting up state

Page 36: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 36/101

LIGHTER STATE MANAGEMENTLIGHTER STATE MANAGEMENTHigher Order ComponentLocal StorageRoute ParameterDatabase (if you are already using it)

ema­john

Page 37: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 37/101

SUMMARYSUMMARY1. 2. 3. Find where to put your state4. 5. You can apply ideas from Redux without using it6. Come back to Redux if you find a real need for it

Think in ReactRead about composition

You Might Not Need Redux

React state without Redux

Page 38: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 38/101

Page 39: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 39/101

Page 40: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 40/101

Page 41: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 41/101

Page 42: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 42/101

QUIZQUIZWho was the guy knocking at the door?

Page 43: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 43/101

USE CASE­3USE CASE­3

STATE MANAGEMENT LIBRARY:STATE MANAGEMENT LIBRARY:REDUXREDUX

When you need to share state a lot

Page 44: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 44/101

Page 45: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 45/101

Page 46: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 46/101

Page 47: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 47/101

Page 48: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 48/101

Page 49: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 49/101

WHAT IF WE PUT THEM IN ONEWHAT IF WE PUT THEM IN ONEPLACEPLACE

Page 50: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 50/101

Page 51: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 51/101

Page 52: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 52/101

Page 53: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 53/101

Page 54: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 54/101

STORESTOREHolds application stateOnly one store for all its application stateSingle source of truth

Page 55: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 55/101

Page 56: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 56/101

HOW WILL WE CHANGE THEHOW WILL WE CHANGE THESTATE?STATE?

Page 57: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 57/101

Page 58: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 58/101

ACTIONACTIONActionsAction container

dispatch action

{ type: 'ADD_TODO', text: 'Take 8 bathroom breaks' }

Page 59: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 59/101

Page 60: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 60/101

BUT YOU DIDN'T TELL HOW STATEBUT YOU DIDN'T TELL HOW STATEWILL CHANGE?WILL CHANGE?

Page 61: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 61/101

ACTUALLY STATE WILL NOTACTUALLY STATE WILL NOTCHANGECHANGE

WE WILL CREATE A NEW STATE WITHWE WILL CREATE A NEW STATE WITHTHE CHANGETHE CHANGE

function add(x, y){ return x + y; }

add(1, 2); // output is 3 add(1, 2); // output still is 3 add(1, 2); // WILL ALWAYS output 3

Page 62: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 62/101

Page 63: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 63/101

Page 64: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 64/101

REDUCERREDUCERActions describe the fact that something happenedAction don't specify how the application's stateshould changeHow state will change will be defined by reducer

case ADD_TODO: return [ ...todos, { text: action.text, completed: false } ]

Page 65: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 65/101

REDUCERREDUCER

Page 66: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 66/101

DATA FLOWDATA FLOWAction is a plain object describing what happenedCall store.dispatch(action) from anywhere in yourappStore calls the reducer function you gave for thegiven actionIn root reducer you may combine multiplereducers into a single state treeStore saves the complete state tree returned by theroot reducer

Page 67: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 67/101

BASIC BUILDING BLOCKBASIC BUILDING BLOCKStoreActionDispatchReducer

Page 68: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 68/101

IMPLEMENT REDUXIMPLEMENT REDUX

npm install --save redux npm install --save react-redux

Page 69: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 69/101

ref: unit test ract redux app

Page 70: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 70/101

Page 71: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 71/101

SEE SOME CODESEE SOME CODE

github.com/khan4019/redux­todo

state management

Page 72: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 72/101

Page 73: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 73/101

QUIZQUIZ

What's the name of the other speaker?

Page 74: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 74/101

USE CASE ­4USE CASE ­4

SCALE WITH REDUXSCALE WITH REDUXWhen you are building really large App

Page 75: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 75/101

REDUX IS GOOD..REDUX IS GOOD..1. Keep an history of your app2. Time-travel over the actions3. Even store the app state in the local storage

But it's not a silver bullet

Redux Dev tool

Page 76: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 76/101

Page 77: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 77/101

MESSED UP WITH REDUXMESSED UP WITH REDUXDo you have too many global action creators?Do you have a huge reducer that manageseverything?Do you have conflicts between your actions orreducers that modify the same piece of state?

redux scaleable architecure

Page 78: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 78/101

Page 79: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 79/101

CONSIDERCONSIDER

Redux actions creators and reducers with featureCreate Data module for data that is not tightlybound to a container componentPut distinct domains into sub-store and splitreducersIsolate network communication in one placeUse Redux-saga or thunk (middleware)Use to pass bits of state to componentsConsider to optimize performance

,

selectorreselect 

slice reducer Destructuring react store

Page 80: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 80/101

IS REDUX THE ONLY OPTION?IS REDUX THE ONLY OPTION?

Page 81: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 81/101

MOBX IS ONE OPTIONMOBX IS ONE OPTION

Redux MobXsingle store multiple storesflat state nested statefunctionalprogramming

OOP & reactiveprogramming

immutable mutableplain JavaScript “magic” JavaScriptmore boilerplate less boilerplate

, , MobX Redux or MobX MobX vs Redux

Page 82: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 82/101

OTHER OPTIONSOTHER OPTIONS

, Relay/Apollo & GraphQLUnstated Explore unstatedRematch: Redux best practices without theboilerplateLocal StorageJust React

Page 83: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 83/101

USE CASE­5USE CASE­5

REDUX SAGAREDUX SAGAWhen you need to handle async call with Redux

Page 84: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 84/101

REDUX­SAGAREDUX­SAGAHandles application side effectsAsynchronous things like data fetchingAccess the browser cache

Page 85: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 85/101

REDUX­SAGAREDUX­SAGAA separate thread in your applicationA redux middlewareStart, pause or cancel by redux actionsCan access redux stateCan dispatch redux actions

Page 86: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 86/101

ES6 GENERATORES6 GENERATOR

function *foo() { yield 1; yield 2; yield 3; yield 4; yield 5; }

var fooed = foo(); fooed.next(); //{value: 1, done: false} fooed.next(); //{value: 2, done: false} fooed.next(); //{value: 3, done: false} fooed.next(); //{value: 4, done: false} fooed.next(); //{value: 5, done: false} fooed.next(); //{value: undefined, done: true}

Page 87: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 87/101

SAGA HELPERSSAGA HELPERSput: dispatches an action.select: get data from the state fork: performs a non-blocking operationcall: runs a function. If it returns a promise,pauses the saga until the promise is resolved. take: pauses until action received.takeEvery: return results for all the callstriggered.takeLatest: If we trigger several cases, it’s goingto ignore all of them except the last one

Page 88: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 88/101

CREATE SAGACREATE SAGA

function* loadUser() { try { //1st step: call getUser, then assign value to user const user = yield call(fetch,'/getUser',{method:'GET'}); //2nd step: dispatch action yield put({type: 'FETCH_USER_SUCCESS', payload: user}); } catch(error) { //optional step: if something fails dispatch fail yield put({type: 'FETCH_FAILED', error}); } }

Page 89: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 89/101

REGISTER SAGA GENERATORREGISTER SAGA GENERATOR

function* sagas() { yield[ fork(loadUser), takeLatest('LOAD_DASHBOARD', loadDashboardSequenced) ]; }

Page 90: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 90/101

INJECT THE SAGA MIDDLEWAREINJECT THE SAGA MIDDLEWARE

import { applyMiddleware, createStore } from 'redux'; import createSagaMiddleware from 'redux-saga'; import reducers from './reducers'; import sagas from './sagas'; const sagaMiddleware = createSagaMiddleware(); let middleware = applyMiddleware(sagaMiddleware); const store = createStore(reducers, middleware); sagaMiddleware.run(sagas); // inject sagas into the middleware sagaMiddleware.run(sagas);

Page 91: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 91/101

ADVANCED USAGEADVANCED USAGENesting SagasSequential SagasRace sagaMultiple SagasTask CancellationTesting Saga

Page 93: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 93/101

FREE TIPFREE TIP

Page 94: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 94/101

4 STEPS4 STEPS

TO APPEAR SMARTTO APPEAR SMART

IN A MEETINGIN A MEETING

Page 95: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 95/101

STEP ­ 1STEP ­ 1

Sleep first 30min

Or use your cell phone

Page 96: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 96/101

STEP­2STEP­2

Page 97: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 97/101

STEP­3STEP­3

Page 98: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 98/101

STEP­4STEP­4

Spend 50min to talk about the Sweet spotAsk the junior developer to reschedule the meeting

Page 99: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 99/101

7 TIPS7 TIPS

TO APPEAR BUSYTO APPEAR BUSY

AT WORKAT WORK

Page 100: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 100/101

APPEAR BUSY AT WORKAPPEAR BUSY AT WORK

1. Ctrl + 1 when big brother is around

2. Always carry extra bag and print out3. Don't leave before ur manager

4. Dont use the same rest room in a row5. Do lunch at your desk not break room

6. Always leave a extra jacket on your chair7. Compose email during office hours, send it

midnight or weekendsLook busy

Page 101: MA NAGE STATE - International Javascript …...4/11/2018 Managing State of a Large React Application file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 1/101 MA

4/11/2018 Managing State of a Large React Application

file:///Users/jhankarmahbub/Documents/git/manage-state/index.html#/12 101/101

THANK YOUTHANK YOU -Slides:

-Website:

-Youtube:

-Email:

khan4019.github.io/manage­state

thatjsude.com

youtube.com/c/ThatJSDude

[email protected]


Recommended