Beyond React
You might not be aware that an import is global already. If you export an object (singleton) it is then globally accessible as an import statement and it can also be modified globally.
If you want to initialize something globally but ensure its only modified once, you can use this singleton approach that initially has modifiable properties but then you can use Object.freeze after its first use to ensure its immutable in your init scenario.
const myInitObject = {}
export default myInitObject
then in your init method referencing it:
import myInitObject from './myInitObject'
myInitObject.someProp = 'i am about to get cold'
Object.freeze(myInitObject)
The myInitObject will still be global as it can be referenced anywhere as an import but will remain frozen and throw if anyone attempts to modify it.
Example of react state using singleton
https://codesandbox.io/s/adoring-architecture-ru3vt (see UserContext.tsx)
If using react-create-app
(what I was looking for actually) In this scenario you can also initialize global objects cleanly when referencing environment variables.
Creating a .env file at the root of your project with prefixed REACT_APP_ variables inside does quite nicely. You can reference within your JS and JSX process.env.REACT_APP_SOME_VAR as you need AND it's immutable by design.
This avoids having to set window.myVar = %REACT_APP_MY_VAR% in HTML.
See more useful details about this from Facebook directly:
https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
Answer from King Friday on Stack OverflowBeyond React
You might not be aware that an import is global already. If you export an object (singleton) it is then globally accessible as an import statement and it can also be modified globally.
If you want to initialize something globally but ensure its only modified once, you can use this singleton approach that initially has modifiable properties but then you can use Object.freeze after its first use to ensure its immutable in your init scenario.
const myInitObject = {}
export default myInitObject
then in your init method referencing it:
import myInitObject from './myInitObject'
myInitObject.someProp = 'i am about to get cold'
Object.freeze(myInitObject)
The myInitObject will still be global as it can be referenced anywhere as an import but will remain frozen and throw if anyone attempts to modify it.
Example of react state using singleton
https://codesandbox.io/s/adoring-architecture-ru3vt (see UserContext.tsx)
If using react-create-app
(what I was looking for actually) In this scenario you can also initialize global objects cleanly when referencing environment variables.
Creating a .env file at the root of your project with prefixed REACT_APP_ variables inside does quite nicely. You can reference within your JS and JSX process.env.REACT_APP_SOME_VAR as you need AND it's immutable by design.
This avoids having to set window.myVar = %REACT_APP_MY_VAR% in HTML.
See more useful details about this from Facebook directly:
https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
Why don't you try using Context?
You can declare a global context variable in any of the parent components and this variable will be accessible across the component tree by this.context.varname. You only have to specify childContextTypes and getChildContext in the parent component and thereafter you can use/modify this from any component by just specifying contextTypes in the child component.
However, please take a note of this as mentioned in docs:
Just as global variables are best avoided when writing clear code, you should avoid using context in most cases. In particular, think twice before using it to "save typing" and using it instead of passing explicit props.
reactjs - React when to use global variables instead of states - Stack Overflow
Global variable for react - javascript
How to manage global variables in React?
How to declare global variables in React JS
Videos
The reason why you use state instead of a variable outside the component is because of re-rendering.
If you do not use state, the component will not be updated with the new data returned from your api.
In addition, the correct way to use useEffect is as follows (commented), if you intend to update the data only once.
const [ cityList, setCityList ] = useState([]);
function Component(){
useEffects(()=>{ //<-- remove async here as it's not allowed.
const getData = async () => {
const data = await loadCities();
setCityList(data)
}
getData();
},[]); //<---- add a empty dependency so it only called once when component mounts
...
}
We usually use variable outside component if it doesn't change.
const initialData = ['a','b','c']
component A = () => {
// it's fine to use initialData out of the component because it doesn't change.
}
Of course, there are also times where we can use variable outside the component that can change over time. But only if the rendering of the component does not depend on it. (e.g. using a global variable to track a setTimeout)
Yes, don't use that method.
You can set a state to keep the values if they can change:
function Component() {
const [cityList, setCityList] = useState()
useEffect(async () => {
if (!cityList) {
const response = await loadCities();
setCityList(response);
}
}, [cityList]);
...
}
Check out this example from the React doc: https://reactjs.org/docs/faq-ajax.html#example-using-ajax-results-to-set-local-state
If the data don't change you can declare a variable inside the component:
function Component() {
const cityList = []
...
}
The hook useMemo that you have quoted is an optimization. It can be used to avoid unnecesary computations because it stores a memoized value.
useMemo is helpful when you have expensive calculations that depends on another values.
Check out the official doc about: https://reactjs.org/docs/hooks-reference.html#usememo
It is a terrible idea, but probably the best/easiest way to use a global variable in React is to put it on the window. In a component you could do something like window.topicText="some text" and then access it via window.topicText everywhere else.
Ideally, if you have data, and in your case likely state, that you need to persist from component to component, you should look into something like Redux or Flux. I prefer Redux.
You can try this: Declare your global variable before your main component App.js and pass this variable as props down the tree.
Parent.js
var myVar = {}
class MyComponent extends Component {
...
...
myVar = new Object();
...
...
render() {
return <div>
\\ children
<MyLeftBranch myVar={myVar} />
<MyRightBranch myVar={myVar} />
</div>
}
}
export default MyComponent;
child.js
class Child extends Component {
{ myVar } = this.props;
\\use myVar
}
The global scope in React Native is variable global. For ex: as global.foo = foo, then you can use global.foo anywhere as a global variable.
The global scope may be used to store the global config or similar things. Share variables between different views, as your description, you can choose many other solutions(use redux,flux or store them in a higher component), the global scope is not a good choice.
A good practice to define global variable is to use a js file. For example global.js
global.foo = foo;
global.bar = bar;
Then, to make sure it is executed when project initialized. For example, import the file in index.js:
import './global.js'
// other code
Have a look at react Context :
https://reactjs.org/docs/context.html
simple example:
const ThemeContext = React.createContext('name');
if you are using react 16.2 and lower use this legacy react context:
https://reactjs.org/docs/legacy-context.html
You can declare a global context variable in any of the parent components and this variable will be accessible across the component tree by this.context.name. You only have to specify childContextTypes and getChildContext
or if you want the "ugly" way, do this: inside vars.js
declare var Name = 'empty';
export default window.Name;
then import './vars' in the file that contains "DataAreaOne" and "DataAreaTwo"
import Name from './vars';
then inside the class
name = Name;
and use it like so
...
if(this.name != "something"){
...
Hi i have a package A with some helper functions and UI component, package A exports UI component and helper functions idependent of each other. I am importing this package in a react app. In the package A i want to initialise a global variable based on which I want to modify the behaviour of some helper functions. what is the best way to set that variable from react app? should i create a global config object and pass it from the react app or is there any better way to handle this problem?