How can I avoid renders using updateOne from createEntityAdapter #1229
-
Hello everyone ! 🙂 Reducerconst myAdapter = createEntityAdapter();
const { actions, reducer } = createSlice({
name: 'mySlice',
initialState: myAdapter.getInitialState(),
reducers: {
updateOne: myAdapter.updateOne,
},
});
export const { updateOne } = actions;
export const mySelectors = myAdapter.getSelectors((state) => state.mySlice);
export default reducer; Component Aconst A = () => <button onClick={() => updateOne({ id, changes: { someData } })}>Some button</button> Component Bconst B = () => {
// allData is considered different every time I click on component's A button
const allData = useSelector((state) => mySelectors.selectEntities(state));
useEffect(() => {
functionUsingMyData(allData);
}, [allData]);
....
} The data store in my store looks like this: [key: string]: string | number | boolean The issue I have is that when I call Do you guys have any idea how I can avoid this ? Thank you for taking the time 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is normal behavior for immutable updates. An immutable update requires that every level of nesting that you are trying to update has to be copied. So, if I want to update
So, yes, if you are trying to select What are you trying to accomplish in that effect? |
Beta Was this translation helpful? Give feedback.
This is normal behavior for immutable updates.
An immutable update requires that every level of nesting that you are trying to update has to be copied. So, if I want to update
state.todos.entities["abcd"].completed
, it has to copy:{id: "abcd"}
entity objectentities
todos
state
So, yes, if you are trying to select
state.someSlice.entities
, that will be a new reference every time you apply an update. That means that the component will re-render, and if you're using that as an effect dependency, the effect will run too.What are you trying to accomplish in that effect?