[英]Call a functional component in React before it is declared
const ShowHide = () => {
return <Item/>;
};
const Item = () => {
return(
//few lines of code
);
};
这里,功能组件 ShowHide 调用组件“Item”。 项目是一个箭头 function,它没有被吊起。
怎么可能在 Item 被声明之前调用它?
几乎是ASDFGerte
评论的内容。
以下将不起作用:
const ShowHide = () => { return Item(); }; console.log(ShowHide()); const Item = () => { return 'hello world' };
因为您试图在定义之前使用 const 并且您可以看到它不会被提升。
以下将起作用:
const ShowHide = () => { return Item(); }; const Item = () => { return 'hello world' }; console.log(ShowHide());
因为在调用ShowHide
并且需要Item
时,它已经被定义了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.