Skip to content

Instantly share code, notes, and snippets.

@SunnyChopper
Created January 18, 2022 01:30
Show Gist options
  • Save SunnyChopper/a8a06047de33650e71a3f7c4dd75fa5b to your computer and use it in GitHub Desktop.
Save SunnyChopper/a8a06047de33650e71a3f7c4dd75fa5b to your computer and use it in GitHub Desktop.
Setting up a Slice with RTK
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