zil*_*Boy 12 reactjs react-redux
有没有什么方法可以在不使用useSelector 的情况下在 React 组件中使用它来获取状态值
import { superAdmin, zoneManager } from './navigations'
import { useSelector } from 'react-redux'
const access = useSelector(state => state.access)
return access
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
无效的挂钩调用。钩子只能在函数组件的主体内部调用。
我明白了,但是有没有其他方法可以在不使用useSelector的情况下获取存储值
谢谢
Mic*_*sen 15
为了使商店可以在任何地方访问(无论出于何种原因),您首先需要从创建商店的文件中导出商店:
// fileThatMakesStore.js
export const store = configureStore();
Run Code Online (Sandbox Code Playgroud)
然后,回到文件中,您可以简单地导入 store 和 getState()。
import { store } from 'fileThatMakesStore.js';
const someValue = store.getState().something;
const someValue = store.getState().someReducer.something; // If you have multiple reducers
Run Code Online (Sandbox Code Playgroud)
很高兴知道:商店只是一个简单的 JS 对象,具有三个功能:subscribe(), dispatch(), getState().