gpt4 book ai didi

reactjs - 不使用 useSelector 获取状态值

转载 作者:行者123 更新时间:2023-12-01 23:16:10 25 4
gpt4 key购买 nike

有没有办法在不使用 useSelector 而在 React Component 中使用它来获取状态值

import { superAdmin, zoneManager } from './navigations'
import { useSelector } from 'react-redux'

const access = useSelector(state => state.access)
return access

我得到的错误是:

Invalid hook call. Hooks can only be called inside of the body of afunction component.

我明白了,但是有没有其他方法可以在不使用 useSelector

的情况下获取存储值

谢谢

最佳答案

要使商店在任何地方都可以访问(无论出于何种原因),您首先需要从您创建商店的文件中导出商店:

// fileThatMakesStore.js
export const store = configureStore();

然后,回到您的文件中,您可以简单地导入商店和 getState()。

import { store } from 'fileThatMakesStore.js';

const someValue = store.getState().something;
const someValue = store.getState().someReducer.something; // If you have multiple reducers

提示:商店只是一个简单的 JS 对象,具有三个函数:subscribe()、dispatch()、getState()

关于reactjs - 不使用 useSelector 获取状态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68799543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com