Skip to content
Advertisement

JavaScript instantiates class without usage React Native

I have the next problem/question in React Native:

I have the next class, let’s say it A that looks like this:

JavaScript

My A class in used in B class like this:

JavaScript

And the makeApiRequest function from B class in used in App.js like this:

JavaScript

And this makeRequest function is placed as an onPress action.

My question is: At the first render of the app, my constructor console.log and called function console.log are printed in terminal. Shouldn’t this happen just before I call the makeRequest function is App.js that calls the makeAPIRequest from B class that instantiate the A class?

If someone can explain me the logic behind this, will be pretty appreciated.

Advertisement

Answer

A is instantiated as static dependency of itself so basically that happens just after class is loaded, if you want to make it lazy instantiated you need to do something like:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement