I am trying to figure out a way of using a piece of data returned from RTK query in a slice as default state.
Let’s say I have this data returned from an API call and I use RTK query to get the data.
const data = {
name: "Test Employee",
options: {..},
badges: {..}
age: 26,
id:1,
isEmployee: options && badges
}
NOTE: isEmployee is either true or false based on the options and badges property which is part of the returned data from the API
I am able to use this data in the component by doing this:
const Employee = () => {
const {data} = useGetEmployee(null)
return <div>{data.name}</div>
}
Now, I have a component where I need to do some conditional rendering based on isEmployee
flag by calling the toggleEmployee
action which basically toggles the isEmployee
flag.
So I create a slice like this:
const initialState = {
isEmployee: ??? // how to set the default state here as it depends on options and badges properties returned from the RTK query
};
export const employeeSlice = createSlice({
name: "employeeSlice",
initialState,
reducers: {
toggleEmployee: (state, action) => {
state.isEmployee = action.payload;
},
},
});
I am new RTK query and trying to figure out what’s the best/recommended way to achieve this. Any help is appreciated, thanks!
1