简体   繁体   English

如何访问 React JS 功能组件中的全局变量?

[英]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.

相关问题 react函数组件中如何实现全局变量 - How to implement global variable in react functional components 如何在反应中从功能组件访问文件 - How to access file from functional component in react 如何将变量导出到另一个反应功能组件? - How to export a variable to another react functional component? React 功能组件,如何从元素 dragStart/dragEnd 回调访问外部 function 变量 - React functional component, how to access outer function variable from element dragStart/dragEnd callback 如何将 function 从 FUNCTIONAL 传递给 CLASS 组件并在渲染外部访问它(没有 prop ),在反应 js 中使用上下文? - How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js? 如何从另一个js文件访问react组件中定义的变量? - How to access a variable defined in a react component from another js file? 如何在React.js组件类中访问变量 - How to access variable in React.js component class 功能性反应组件中的全局范围道具 - Global scope prop in functional react component 如何在功能性反应组件中访问由 CSS 模块设置的散列类名? - How to access the hashed classname set by CSS Modules in a functional react component? 如何在无状态功能组件中访问 props.children? - How to access props.children in a stateless functional component in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM