Redux - ryantc94/Knights-of-Arthur GitHub Wiki
Core Concepts
- Actions are objects that describe changes to state
- Reducers are functions that take state and actions and return next state, with one main reducer that returns entire new state
Three Principles
- Store contains all state
- State is read-only and can only be changed by dispatching action
- Reducers have to be pure functions
Actions
Actions: data payloads that send information from application to the store, and are the only information stores can receive.
- store.dispatch(), dispatches an action to update the store
- Actions are plain javaScript objects, and they must have a property 'type'
{
//'type' is required
type: 'example_action',
text: 'This action is an example'
}
Action Creators
- Action Creators are functions that create actions.
function actionCreator(text) {
return {
type: 'example_action',
text
}
- Action creator dispatch (why cant dispatch be in the action creator and has to be given to dispatch)???
Reducers
Designing State Shape
https://github.com/paularmstrong/normalizr
Handling Actions
Reducers: specifies how the state changes in response to dispatched action.
- Reducers are called reducers because they are the type of function you would pass to Array.prototype.reducer
- Reducers must remain pure: never mutate args, never perform side affects like API calls/routing, never call non-pure functions
- Redux will call reducer with an undefined state for the first time, so the reducer should catch this
- Reducers must assign the state change to a new object or return the original state, it must not mutate the passed in state object
Store
Data Flow
Redux + React
https://github.com/reactjs/react-redux