-1
const ShowHide = () => {
   return <Item/>;
};

const Item = () => {
   return(
     //few lines of code
   );
};

Here, the functional component ShowHide calls component 'Item'. Item is an arrow function and it is not hoisted.

How is it possible to call Item before it is declared?

Jared Smith
  • 14,977
  • 4
  • 36
  • 57

2 Answers2

0

How is it possible to call Item before it is declared?

It's imposible. Read this: javascript variables and temporal dead zone

You can define Item in other file, and imports to file with ShowHide component. It's all what you can do.

FXX058
  • 11
  • 2
0

Pretty much what ASDFGerte commented.

The following would not work:

const ShowHide = () => {
   return Item();
};

console.log(ShowHide());
const Item = () => {
   return 'hello world'
};

Because you are trying to use a const before it's defined and as you can see it won't be hoisted.

The following will work:

const ShowHide = () => {
   return Item();
};
const Item = () => {
   return 'hello world'
};
console.log(ShowHide());

Because by the time ShowHide is called and Item is needed it is already defined.

HMR
  • 30,349
  • 16
  • 67
  • 136
  • Thankyou. The code snippet is used in React and 'Showhide' is exported to app.js.I am not sure how the execution of 'ShowHide' happens in app.js after defining 'Item'.Appreciate your help. – Yearn2Learn Mar 09 '21 at 04:36