反应承诺到useState Hook的终止

我正在使用上下文函数,并且在其他js文件上调用了该函数。但是setThreeImdf是useState函数。

上下文函数;

     const [threeImdf,setThreeImdf] = useState({});

     const createMap = (ref,width,height) => {
          const display_point = map.venue.features[0].properties.display_point.coordinates;
          const threeImdf = new ThreeImdf(
            ref,height,map,display_point
          );
          window.addEventListener("resize",() => {
            threeImdf.resize(window.innerWidth,window.innerHeight);
          });
          threeImdf.showLayer([0]);
          setThreeImdf(threeImdf);
        });
      };

Home.js useEffect函数

const { map,createMap,threeImdf } = useContext(MapContext);

    useEffect( async () => {
        if (!_.isEmpty(map)) {
          createMap(mapRef,window.innerWidth,window.innerHeight);
          const data = threeImdf.getGrouppedObjectsByLayer(currentFloor);
          const layers = threeImdf.getOrdinalsnumber();
          setfloorCount(layers);
          setCategories(data);
        }
      },[]);

但是我在useEffect中运行上下文函数,返回给我threeImdf找不到。我想等待setThreeImdf挂钩的结束。我该怎么办?

qazwsxedcr123 回答:反应承诺到useState Hook的终止

需要创建上下文

usedBudget

上下文可见,您需要将组件包装在以下位置:

const Context = React.createContext(MapContext);
本文链接:https://www.f2er.com/3139753.html

大家都在问