[英]How to access a Global variable in React JS functional component?
I have a global variable in the window object in a functional React component.我在功能性 React 组件的 window object 中有一个全局变量。
It's Global --> Designer --> nodes --> NODE_FORM --> props --> dataSource deep.它是 Global --> Designer --> nodes --> NODE_FORM --> props --> dataSource deep。 How can I access it in my functional component?如何在我的功能组件中访问它?
Global variables are bad design within react.全局变量在反应中是糟糕的设计。 Use Context instead https://reactjs.org/docs/context.html使用上下文代替https://reactjs.org/docs/context.html
Y would you?你会吗? It doesnt make any sense.这没有任何意义。 Alot of anti patterns at onces.一次有很多反模式。 But the way of doing It Is但它的做法是
Global === Window
const Comp = props => {
const [get, set]= React.useState("");
React.useEffect(()=>{
set(window.Designer.nodes.NODE_FROM.dataSource);
},[window.Designer]);
return (<div/>)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.