Skip to content

Instantly share code, notes, and snippets.

@sambhav2612
Last active June 25, 2019 15:48
Show Gist options
  • Save sambhav2612/79b3c6777519a6a4523467d9c1387700 to your computer and use it in GitHub Desktop.
Save sambhav2612/79b3c6777519a6a4523467d9c1387700 to your computer and use it in GitHub Desktop.
/**
* common shareable state
*/
export const INITIAL_STATE = {
userName: null,
userReferralCode: null,
userToken: null,
userSignup: false,
userLoggedIn: false
};
/**
* some reducers
*/
// triggered by userNameSuccess action
export const requestNameSuccess = (action, state) => {
console.warn('saving to name', state['username']);
return Object.assign({}, state, {
userName: state['username']
});
};
// triggered by userTokenSuccess action
export const requestTokenSuccess = (action, state) => {
console.warn('saving to token', state['1fjsdbghfjgsbvfhsfgbzxcvnxhzv']);
return {...state, userToken: state['1fjsdbghfjgsbvfhsfgbzxcvnxhzv']};
};
/**
* in some react component
*/
const mapDispatchToProps = (dispatch) => {
return {
// parameters come by component state
sendName: (name) => {
dispatch(AuthActions.userNameSuccess(name)) // updates state by setting username
},
sendToken: (token) => {
dispatch(AuthActions.userTokenSuccess(refCode)) // overwrites state from beginning irrespective to updates made before by any other action
}
}
};
// in some other component where above updates have been made
const mapStateToProps = (state) => {
console.warn('printing state', state.auth);
return {
name: state.auth.userName,
token: state.auth.userToken // only this is visible in current component's props via above state
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment