Reputation: 365
Yesterday I had an interview for React developer position. Interviewer asked me about global functions and how to create and call global functions with React ?
I thought he is talking about Redux and state containers ...
But he said, no ... it's not about state containers and it's about react services.
Since after interview I searched about react services, but I didn't find any thing.
What do you think about this feature and global functions ? I should import global functions above all components or here is different method to handle this ?
Thanks
Upvotes: 1
Views: 3758
Reputation: 26403
You have to describe what you call a "react service" and "global function" here, then it will be possible to say the answer how to get it done.
Any kind of "global" stuff, like the helper function which you need in many different places I would put in separate file and export it there and then import/require anywhere needed.
Traditionally, in the web app, the global function may be something added to the window object. Which is a bad pattern and it's well documented over the years. Just google on a topic.
"React service", for me, mostly reminds some API that can run server-side render to render the react app. Like API endpoint which you provide ReactJS app URL and it will spit back raw HTML. I would use headless chrome and global function in this context may be js running in page context running inside the chrome but this stuff is definitely out of the scope of ReactJS.
Another thing, it could be any kind of helper functions that provide some kind of data management, connection handling to something or almost whatever else.
I think the correct answer would be: Define what do you mean by "react service" and "global function".
Too many things could be called a service or global function.
Upvotes: 0