Recoil useRecoilValue外部组件



我想基于一堆后坐力值构建一个JSON文件,所以我创建了一个函数来获取所有的后坐力值并从中返回一个JSON,我从一个组件调用它。

事情是,它给了我一个错误使用useRecoilValue的组件。

除了将函数移动到组件内部(这会使组件看起来很糟糕,因为有很多值)或将所有recoilValue作为参数传递给函数(相同)之外,我还能做什么?

这是总体思路-

const ReactCom = () => {
getJson();
....
}
const getJson = () => {
let jsonFile = useRecoilValue(someAtom);
return jsonFile;
}
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a 
function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

应该可以了

const ReactCom = () => {
const json = useGetJson();
....
}
const useGetJson= () => {
let jsonFile = useRecoilValue(someAtom);
return jsonFile;
}

最新更新