Created
January 18, 2022 01:30
-
-
Save SunnyChopper/a8a06047de33650e71a3f7c4dd75fa5b to your computer and use it in GitHub Desktop.
Setting up a Slice with RTK
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 { createSlice } from '@reduxjs/toolkit'; | |
// Make an initial state for Redux | |
const initialVotingPollSlice = { | |
votedPollIds: [], | |
canVote: false, | |
currentVote: null | |
}; | |
// Create a slice using RTK | |
export const votingPollSlice = createSlice({ | |
name: 'polls', | |
initialState: initialVotingPollSlice, | |
reducers: { | |
setCanVote: (state, action) => { | |
state.canVote = action.payload; | |
}, | |
setCurrentVote: (state, action) => { | |
state.currentVote = action.payload; | |
}, | |
addVotedPollId: (state, action) => { | |
state.votedPollIds.append(action.payload); | |
} | |
} | |
}); | |
// Create the action functions, which are generated by `createSlice` using the | |
// reducer functions you define up above. | |
export const { setCanVote, setCurrentVote, addVotedPollId } = votingPollSlice.actions; | |
// Make sure to export your reducer! | |
export default votingPollSlice.reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment