Redux Flashcards
Redux uses pure functions heavily
Pure functions return a new value based on arguments passed to them. They don’t modify existing objects; instead, they return a new one. These functions don’t rely on the state they’re called from, and they return only one and the same result for any provided argument.
Redux has three building parts
actions, store and reducers
actions are events
{ type: LOGIN_FORM_SUBMIT, payload: {username: ‘alex’, password: ‘123456’} }
Actions are created with action creators, which are just
functions that return actions function authUser(form) { return { type: LOGIN_FORM_SUBMIT, payload: form } }
Calling actions anywhere in the app, then, is very easy. Use the
dispatch method, like so: dispatch(authUser(form));
reducers have a somewhat different meaning from functional Javascript (where they take multiple values and consolidate to one, or act upon streams). In Redux . . .
reducers are functions (pure) that take the current state of the application and an action and then return a new state
Here is a very simple reducer that takes the current state and an action as arguments and then returns the next state
function handleAuth(state, action) { return _.assign({}, state, { auth: action.payload }); }
For more complex apps, it is recommended to use the
combineReducers() utility provided by Redux
If an object (state) changes only some values, Redux creates a
new object, the values that didn’t change will refer to the old object and only new values will be created. That’s great for performance
const rootReducer (example)
const rootReducer = combineReducers({ handleAuth: handleAuth, editProfile: editProfile, changePassword: changePassword });
Store is the object that holds the application state and provides a few helper methods to
access the state, dispatch actions and register listeners. The entire state is represented by a single store
Any action returns a
new state via reducers. That makes Redux very simple and predictable.
(simple example of a store)
import { createStore } from ‘redux’; let store = createStore(rootReducer); let authInfo = {username: ‘alex’, password: ‘123456’}; store.dispatch(authUser(authInfo));
Redux has a slightly different implementation of time travel than Flux. In Redux, you can
go back to a previous state and even take your state in a different direction from that point on
Redux DevTools supports the following “time travel” features in the Redux workflow (think of them as Git commands for your state):
Reset: resets to the state your store was created with Revert: goes back to the last committed state Sweep: removes all disabled actions that you might have fired by mistake Commit: makes the current state the initial state
The time-travel feature is not
efficient in production and is only intended for development and debugging. The same goes for DevTools
Redux makes testing much easier because
it uses functional JavaScript as a base, and small independent functions are easy to test. So, if you need to change something in your state tree, import only one reducer that is responsible for that state, and test it in isolation.
How to make the state available to components
A best practice is to push the state down to children components
To make everything work together, we have to register the store listener with a
subscribe helper method
ngrx/store
Store builds on the concepts made popular by Redux, a popular state management container in the React community, and supercharges it with the backing of RxJS.
your store can be thought of as a
client side ‘single source of truth’, or database. By adhering to the 1 store contract when designing your application, a snapshot of store at any point will supply a complete representation of relevant application state