'TypeError:分配的右侧不能被解构'useContext React Hooks

问题描述

我在React Hooks中工作,并遇到了useContext方法的问题。我的MapStyle上下文类(使用Google Maps API)如下:

const [randomID,setRandomID] = useState(null);

useEffect(() => {
    setRandomID(Math.floor(1000 + Math.random() * 9000));
},[]);

我让地图组件尝试访问selectedMapStyle属性

import React,{ createContext,useState } from 'react';

const MapStyleContext = createContext();

function MapStyleProvider(props) {
const [selectedMapStyle,setSelectedMapStyle] = useState();

return (
    <MapStyleContext.Provider value={{ selectedMapStyle,setSelectedMapStyle}}>
        {props.children}
    </MapStyleContext.Provider>
);
}

export { MapStyleContext,MapStyleProvider };

最后,当用户更改单选按钮的值时,我试图在setStyle组件中使用setter setSelectedMapStyle:

...
export default function Map() {
const { isLoaded,loadError } = useLoadScript({
    googleMapsApiKey: process.env.REACT_APP_MAPS_API_KEY
});
const { selectedMapStyle } = useContext(MapStyleContext);
...

我不太确定我要去哪里哪里,但是当我使用此上下文时,上下文道具似乎永远无法正确加载(给我类型错误)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)