REACT STATE MANAGEMENT
WITH REDUX
Vedran Blaženka
Front-End Developer at Enterwell.
JavaScript, React, Redux, CSS Modules,
PostCSS, Elm...
enterwell.net/careers/
STATE MANAGEMENT IS HARD
Tracking server responses
Cached data
Tracking local data
UI state, Show/hide spinner, active
route...
server-side rendering, fetching data
before performing route transitions
etc...
React
Communication between components
PREDICTABLE STATE CONTAINER
Predictable
Make state mutations predictable by putting
restrictions on how and when updates can
happen.
(actions, reducers)
Redux's Three Principles
Single source of truth
The of your whole application is stored in an
object tree within a single .
state
store
State is read-only
The only way to change the state is to emit an ,
an object describing what have or should happened.
action
Changes are made with
pure functions
To specify how the state tree is transformed by actions,
you write pure .reducers
Data Flow
strict unidirectional data flow
Actions
{
type: 'ADD_TODO',
payload: {
text: 'Build my first Redux app'
}
}
https://github.com/acdlite/flux-standard-action
1. Something has (or should) happen
2. Don't specify how
Action Creator
function addTodo(text) {
return {
type: 'ADD_TODO',
payload: {
text
}
}
}
store.dispatch(addTodo('Write blog post'));
Reducer
(previousState, action) => newState
Pure function that takes the previous state,
and an action and returns the new state.
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'));
Things you should never do inside a reducer:
Mutate its arguments;
Perform side effects like API calls and routing
transitions;
Call non-pure functions, e.g. Date.now() or
Math.random().
Just a calculation!
Immutability
No surprises. No side effects. No API
calls. No mutations.
Store
getState()
dispatch(action)
subscribe(listener)
replaceReducer(nextReducer)
State vs. Store
Do whatever is less awkward. - Adam Abramov
http://redux.js.org/docs/faq/OrganizingState.
html#organizing-state-only-redux-state
Marc was almost ready to implement
his "hello world" React/Redux app
TO BE, OR NOT TO BE
(to redux, or not)
Pros:
Total separation of data and UI
Server-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
Cons:
Community conventions are still developing
"Feels bloated, too much boilerplate code..."
¯_( )_/¯ (shrug emoji)
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) :
Hvala na pažnji!
Vedran Blaženka
@vblazenka
blazenka.vedran@gmail.com

React state managmenet with Redux