33
REACT STATE MANAGEMENT WITH REDUX

React state managmenet with Redux

Embed Size (px)

Citation preview

Page 1: React state managmenet with Redux

REACT STATE MANAGEMENTWITH REDUX

Page 2: React state managmenet with Redux
Page 3: React state managmenet with Redux
Page 4: React state managmenet with Redux

Vedran BlaženkaFront-End Developer at Enterwell.

JavaScript, React, Redux, CSS Modules,PostCSS, Elm...

Page 5: React state managmenet with Redux

enterwell.net/careers/

Page 6: React state managmenet with Redux

STATE MANAGEMENT IS HARD

Page 7: React state managmenet with Redux

Tracking server responsesCached dataTracking local dataUI state, Show/hide spinner, activeroute...server-side rendering, fetching databefore performing route transitionsetc...

Page 8: React state managmenet with Redux

ReactCommunication between components

Page 9: React state managmenet with Redux

PREDICTABLE STATE CONTAINER

Page 10: React state managmenet with Redux

Predictable

Make state mutations predictable by puttingrestrictions on how and when updates can

happen.

(actions, reducers)

Page 11: React state managmenet with Redux
Page 12: React state managmenet with Redux

Redux's Three Principles

Page 13: React state managmenet with Redux

Single source of truthThe of your whole application is stored in an

object tree within a single .

statestore

Page 14: React state managmenet with Redux

State is read-onlyThe only way to change the state is to emit an ,an object describing what have or should happened.

action

Page 15: React state managmenet with Redux

Changes are made withpure functions

To specify how the state tree is transformed by actions,you write pure .

reducers

Page 16: React state managmenet with Redux

Data Flowstrict unidirectional data flow

Page 17: React state managmenet with Redux
Page 18: React state managmenet with Redux

Actions{ type: 'ADD_TODO', payload: { text: 'Build my first Redux app' }}

https://github.com/acdlite/flux-standard-action

1. Something has (or should) happen2. Don't specify how

Page 19: React state managmenet with Redux

Action Creator

function addTodo(text) { return { type: 'ADD_TODO', payload: { text } }}

store.dispatch(addTodo('Write blog post'));

Page 20: React state managmenet with Redux

Reducer

(previousState, action) => newState

Pure function that takes the previous state,and an action and returns the new state.

Page 21: React state managmenet with Redux
Page 22: React state managmenet with Redux

function todoReducer(state = [], action) { switch(action.type) { case 'ADD_TODO': return [ ...state, action.payload ]; case default: return state; }}

export default todoReducer;

function addTodo(text) { return { type: 'ADD_TODO', payload: { text } }}

store.dispatch(addTodo('Write blog post'));

Page 23: React state managmenet with Redux

Things you should never do inside a reducer:

Mutate its arguments;Perform side effects like API calls and routingtransitions;Call non-pure functions, e.g. Date.now() orMath.random().

Page 24: React state managmenet with Redux

Just a calculation!

Immutability

No surprises. No side effects. No APIcalls. No mutations.

Page 26: React state managmenet with Redux

State vs. Store

Do whatever is less awkward. - Adam Abramov

http://redux.js.org/docs/faq/OrganizingState.html#organizing-state-only-redux-state

Page 27: React state managmenet with Redux

Marc was almost ready to implementhis "hello world" React/Redux app

Page 28: React state managmenet with Redux
Page 29: React state managmenet with Redux

TO BE, OR NOT TO BE(to redux, or not)

Page 30: React state managmenet with Redux

Pros:Total separation of data and UIServer-side rendering

Developer Experience (DevTools, Time Travel, Logging)Growing ecosystem

Easy error reporting integration with other services(Sentry)Strinct unidirectional data flow

Redux API - 99 lines of code

Page 31: React state managmenet with Redux

Cons:Community conventions are still developing

"Feels bloated, too much boilerplate code..."¯\_( )_/¯ (shrug emoji)

Page 32: React state managmenet with Redux

http://redux.js.org/

https://egghead.io/courses/getting-started-

with-redux

https://facebook.github.io/immutable-js/

https://github.com/reactjs/react-redux

https://github.com/gaearon/redux-thunk

Resources (za one koji žele znati više) :

Page 33: React state managmenet with Redux

Hvala na pažnji!

Vedran Blaženka

@vblazenka

[email protected]