Home > Mobile >  How to access a Global variable in React JS functional component?
How to access a Global variable in React JS functional component?

Time:12-17

I have a global variable in the window object in a functional React component.

enter image description here

It's Global --> Designer --> nodes --> NODE_FORM --> props --> dataSource deep. How can I access it in my functional component?

CodePudding user response:

Global variables are bad design within react. Use Context instead https://reactjs.org/docs/context.html

CodePudding user response:

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/>)
};
  • Related