Skip to content

Instantly share code, notes, and snippets.

@thesabbir
Last active September 6, 2018 20:53
Show Gist options
  • Save thesabbir/4d4228c9c6fd7b415fd52d4f19f2323f to your computer and use it in GitHub Desktop.
Save thesabbir/4d4228c9c6fd7b415fd52d4f19f2323f to your computer and use it in GitHub Desktop.
Redux with example: Share state between two reducers https://thesabbir.com/redux-with-example-share-state-between-two-reducers
// using redux thunk async action
export function initiateNewChat(id, message) {
return (dispatch, getState) => {
// get friend from friends store
const friend = getState()
.friends[id];
if (friend) {
// if friend exists we initiate new chat
return {
type: 'INITIATE_CHAT',
friend,
message
};
}
// if not we return a error action
return {
type: 'ERROR_CREATING_CHATBOX',
}
};
}
// state = chatoboxes
export function chatBoxReducer(state = {}, action) {
switch (action.type) {
case 'INITIATE_CHAT':
// We have friends data with action.friend
return state;
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment