-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
redux #15
Comments
AdvancedMiddlewarefunction compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
export default function applyMiddleware(...middlewares) {
return (createStore) => (reducer, preloadedState, enhancer) => {
const store = createStore(reducer, preloadedState, enhancer)
let dispatch = store.dispatch
let chain = []
const middlewareAPI = {
getState: store.getState,
// use local dispatch reference
// make sure dispatch inside of middleware travel the whole middleware chain again
dispatch: (action) => dispatch(action)
}
chain = middlewares.map(middleware => middleware(middlewareAPI))
dispatch = compose(...chain)(store.dispatch)
return {
...store,
dispatch
}
}
} Async ActionsWhen you calls an asynchronous API, There are two crucial moments in time: the moment you start the call, and the moment when you receive an answer (or a timeout). Each of these two moments usually require a change in application state. So , there are at least three different kinds of actions:
Async Action Creators
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
// next is next middleware or store.dispatch
// came from funcs.reduce((a, b) => (...args) => a(b(...args)))
return next(action);
};
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
import { isFSA } from 'flux-standard-action';
function isPromise(val) {
return val && typeof val.then === 'function';
}
export default function promiseMiddleware({ dispatch }) {
return next => action => {
if (!isFSA(action)) {
return isPromise(action)
? action.then(dispatch)
: next(action);
}
return isPromise(action.payload)
? action.payload.then(
result => dispatch({ ...action, payload: result }),
error => {
dispatch({ ...action, payload: error, error: true });
return Promise.reject(error);
}
)
: next(action);
};
} Async FlowWithout You may enhance Asynchronous middleware like When the last middleware in the chain dispatches an action, it has to be a plain object. This is when the synchronous Redux data flow takes place. |
redux-observable |
Redux
createStore
If
enhancer
is provided,createStore
will becomeenhancer(createStore)
.Actions
Actions are the only source of information for the store. You send them to the store using
store.dispatch()
Action Creators
Action Creators are functions that create actions. In traditional Flux, action creators often trigger a dispatch when invoked, But not in Redux. You should explicit call the dispatch function or create
bound action creator
that automatically dispatches.Action Creators can be asynchronous and have side-effects.
Reducers
Reducers descibe how application's state changes when actions dispatch.
The reducer is a pure function that takes the previous state and an action, and returns the next state.
Reducer function must be pure function, given the same arguments, it should calculate the next state and return it. No surprises. No side effects. No API calls. No mutations. Just a calculation.
Splitting Reducers
As your application grow , you single reducer function will also grow to very long.
Redux provide
combineReducers()
.Data Flow
React
Technically you could write the container components by hand using
store.subscribe()
. We don't advise you to do this because React Redux makes many performance optimizations that are hard to do by hand. For this reason, rather than write container components, we will generate them using theconnect()
function provided by React Redux.connect
connect()
takesmapStateToProps
andmapDispatchToProps
and conver a presentational component to container component.The text was updated successfully, but these errors were encountered: