How to get state variable from "A" slice in "B" slice? #54
-
Hi, everyone |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I think it's impossible |
Beta Was this translation helpful? Give feedback.
-
You're right that using the useSelector hook within a slice file is not the correct approach. Redux slices are supposed to be isolated and shouldn't depend directly on React-specific hooks like useSelector. However, there are proper ways to share and access data across slices. Here are very simple and common way. // sliceA.js export const selectSliceAData = (state) => state.sliceA.data; If you need to access data from another slice during an asynchronous operation, you can use getState within a thunk to fetch the data from the store. export const fetchAndModifyData = () => (dispatch, getState) => { // Perform operations using dataFromSliceA and then dispatch actions in sliceB export default sliceB.reducer; |
Beta Was this translation helpful? Give feedback.
-
We can think slice A as a module. |
Beta Was this translation helpful? Give feedback.
We can think slice A as a module.
So we can export slice A and in slice B, we can import slice A.
with:
import sliceA from '../sliceB/sliceB.js"