Last active
August 6, 2020 01:55
-
-
Save Rajatgms/f4840bcf44874b5c60b9686296b573c6 to your computer and use it in GitHub Desktop.
reducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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