Reactjs 应提供部件声明;函数vs变量的返回值
我正在尝试渲染ReactDOM渲染函数中的组件集合。当我声明一个返回这些组件列表的函数时,一切都很好Reactjs 应提供部件声明;函数vs变量的返回值,reactjs,ecmascript-6,Reactjs,Ecmascript 6,我正在尝试渲染ReactDOM渲染函数中的组件集合。当我声明一个返回这些组件列表的函数时,一切都很好 const GlobalComponent = () => <React.Fragment><GlobalStyle/><App/></React.Fragment>; ReactDOM.render(<GlobalComponent/>, document.getElementById('root')); const Glob
const GlobalComponent = () => <React.Fragment><GlobalStyle/><App/></React.Fragment>;
ReactDOM.render(<GlobalComponent/>, document.getElementById('root'));
const GlobalComponent=()=>;
ReactDOM.render(,document.getElementById('root'));
但当我尝试将
分配给对象时,我收到一个错误
元素类型无效:应为字符串(对于内置组件)
或者一个类/函数(用于复合组件),但得到:object
错误代码:
const GlobalComponent = <React.Fragment><GlobalStyle/><App/></React.Fragment>;
ReactDOM.render(<GlobalComponent/>, document.getElementById('root'));
常量GlobalComponent=;
ReactDOM.render(,document.getElementById('root'));
有人能解释一下为什么我无法将React组件分配给对象吗?您不需要
<../>在第二个示例中,代码>关于GlobalComponent
,因为当您将该组件分配给GlobalComponent
变量时,该组件已经创建。有关详细信息,请查看确定,谢谢。现在我可以看到第一个示例(整个GlobalComponent函数)被视为用户定义的组件。实际上,函数的结果被视为一个变量(而不是
,我应该调用GlobalComponent()
,它将返回React.Fragment
)不,不是GlobalComponent()
只是GlobalComponent
不是一个函数,它是您创建并指定给变量的组件。因此,您可以像传递任何其他类型的变量一样传递变量。您不需要<..../>在第二个示例中,代码>关于GlobalComponent
,因为当您将该组件分配给GlobalComponent
变量时,该组件已经创建。有关详细信息,请查看确定,谢谢。现在我可以看到第一个示例(整个GlobalComponent函数)被视为用户定义的组件。实际上,函数的结果被视为一个变量(而不是
,我应该调用GlobalComponent()
,它将返回React.Fragment
)不,不是GlobalComponent()
只是GlobalComponent
不是一个函数,它是您创建并指定给变量的组件。因此,您可以像传递任何其他类型的变量一样传递变量。