Replies: 2 comments 4 replies
-
Why not create the special value "all"? Your slice probably doesn't really need to know all those IDs. |
Beta Was this translation helpful? Give feedback.
-
OK, let me share my version. It works; that is not the problem. The question is if this is the correct/Redux Toolkit/RTK agnostic way of doing it. Because I personally have many doubts. // selected-brides-slice.ts
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../../app/store";
import { Breed } from "./dogs-slice";
interface SelectedState {
selectedBreedIds: {
[k: string]: boolean;
};
}
const initialState: SelectedState = {
selectedBreedIds: {},
};
export const allBreedsSelectionChanged = createAsyncThunk(
`selectedBridesSlice/allBreedsSelectionChanged`,
async (_, thunkAPI) => {
// TODO: How-to property typescript-type these?
const rootState = thunkAPI.getState() as RootState;
const data = (rootState.api.queries["fetchBreeds(undefined)"]?.data as Breed[]) ?? [];
// read all breed ids from `apiSlice`
return data.map((breed: Breed) => breed.id);
}
);
const selectedBridesSlice = createSlice({
name: "selectedBridesSlice",
initialState,
extraReducers: (builder) => {
builder.addCase(allBreedsSelectionChanged.fulfilled, (state, action) => {
if (Object.keys(state.selectedBreedIds).length <= 0) {
// select all
action.payload.forEach((id) => {
state.selectedBreedIds[id] = true;
});
} else {
// de-select all
state.selectedBreedIds = {};
}
});
},
reducers: {
breedSelected(state, action: PayloadAction<string>) {
state.selectedBreedIds[action.payload] = true;
},
breedDeselected(state, action: PayloadAction<string>) {
delete state.selectedBreedIds[action.payload];
},
},
});
export const { breedSelected, breedDeselected } = selectedBridesSlice.actions;
export default selectedBridesSlice.reducer;
// -----------------------------------
// usage:
dispatch(allBreedsSelectionChanged()); How it works It uses My doubts
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to use RTK Query and Redux Toolkit the way it was designed. But already, with a very simple example, I came across the need to share data between slices / reducers (considered as anti-pattern, of course).
Let's take for example the application from this video ( Let’s Learn Modern Redux! (with Mark Erikson) ; source code: https://github.com/learnwithjason/lets-learn-redux-toolkit ). A simple application that loads and displays a list of dog breeds. Easy.
I want to implement a common "Select dog" feature - a simple checkbox next to each dog, where the end-user can select any number of dogs he likes. (Imagine selecting multiple emails in GMail for example).
As list of dogs is coming from RTK (createApi()), I have to create a new slice (
createSlice()
) that will store id's of currently selected dogs. Easy, it could look like this:But now, I want to implement 'Select All' and 'Deselect All' functionality ( again, imagine GMail functionality ).
I want to follow Redux Style Guide - keep as much logic in reducers as possible, do not send a list of ids "from-the-frontend", ...
How to load a list of all breed ids from
apiSlice
inselectedBridesSlice
?Beta Was this translation helpful? Give feedback.
All reactions