Skip to content

Instantly share code, notes, and snippets.

@Rajatgms
Last active August 6, 2020 01:55
Show Gist options
  • Save Rajatgms/f4840bcf44874b5c60b9686296b573c6 to your computer and use it in GitHub Desktop.
Save Rajatgms/f4840bcf44874b5c60b9686296b573c6 to your computer and use it in GitHub Desktop.
reducers
import { NOTIFY_SUCCESS, NOTIFY_ERROR, NOTIFY_RESET } from '../actions/notifyAction';
const initialAlert = {
variant: '',
message: '',
};
const notifyReducer = (alert = initialAlert, action) => {
if (action.type === NOTIFY_SUCCESS) {
return { ...alert, variant: 'success', message: action.payload};
} else if (action.type === NOTIFY_ERROR) {
return { ...alert, variant: 'danger', message: action.payload};
} else if (action.type === NOTIFY_RESET) {
return initialAlert;
}
return alert;
};
export default notifyReducer;
import { notifyErrorAction, notifyResetAction, notifySuccessAction } from '../actions/notifyAction';
import { createReducer } from '@reduxjs/toolkit';
const initialAlert = {
variant: '',
message: '',
};
const notifyReducer = createReducer(initialAlert, builder => {
builder
.addCase(
notifySuccessAction,
(state, action) => ({ variant: 'success', message: action.payload }),
)
.addCase(
notifyErrorAction,
(state, action) => ({ variant: 'danger', message: action.payload }),
)
.addCase(
notifyResetAction,
() => initialAlert,
);
});
export default notifyReducer;
import { notifyErrorAction, notifyResetAction, notifySuccessAction } from '../actions/notifyAction';
import { createReducer } from '@reduxjs/toolkit';
const initialAlert = {
variant: '',
message: '',
};
const notifyReducer = createReducer(initialAlert, {
[notifySuccessAction]: (state, action) => (
{ variant: 'success', message: action.payload }
),
[notifyErrorAction]: (state, action) => (
{ variant: 'danger', message: action.payload }
),
[notifyResetAction]: () => initialAlert,
});
export default notifyReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment