[英]How can I render a function on a stateless component just below another component within a conditional?
I have a stateless component where I am using a conditional to render some components: 我有一个无状态组件,在其中使用条件渲染一些组件:
<div className="bx--col-md-12 bx--offset-xxl-1 bx--col-xxl-10">
{softlayerAccountId ? (
<TableToolbarComp />
renderComponents()
) : (
<div className="login-settings__message">
<UpgradeMessage />
</div>
)}
</div>
When <TableToolbarComp />
and renderComponents()
were not inside the conditional, it was working good, but now it is throwing a parsing error message. 当
<TableToolbarComp />
和renderComponents()
不在条件中时,它可以正常工作,但是现在它抛出了解析错误消息。
What can I do? 我能做什么?
Error: 错误:
>SyntaxError: /Users/marceloretana/Documents/forks/billing/src/client/pages/Cancellations/index.js: Unexpected token, expected "," (82:14)
80 | {softlayerAccountId ? (
81 | <TableToolbarComp />
> 82 | renderComponents()
| ^
83 | ) : (
84 | <div className="login-settings__message">
85 | <UpgradeMessage />
Even if I set {}
around renderComponents()
I get this 即使我在
renderComponents()
周围设置了{}
,我也得到了
SyntaxError: /Users/marceloretana/Documents/forks/billing/src/client/pages/Cancellations/index.js: Unexpected token, expected "," (82:14)
80 | {softlayerAccountId ? (
81 | <TableToolbarComp />
> 82 | { renderComponents() }
| ^
83 | ) : (
The ()
is the grouping operator, and you cannot write both JSX and plain JS in the same group. ()
是分组运算符,您不能在同一组中同时编写JSX和普通JS。 The JS ( renderComponents()
) has to be escaped using curly braces {}
. JS(
renderComponents()
)必须使用花括号{}
进行转义。 Furthermore, you cannot return multiple containing elements from the group, so you either have to wrap it in a containing element (ie a div
) or if you don't want additional markup, you can use React.Fragment : 此外,您不能从组中返回多个包含元素,因此您必须将其包装在一个包含元素中(即
div
),或者如果您不需要其他标记,则可以使用React.Fragment :
<div className="bx--col-md-12 bx--offset-xxl-1 bx--col-xxl-10">
{softlayerAccountId ? (
<React.Fragment>
<TableToolbarComp />
{renderComponents()}
</React.Fragment>
) : (
<div className="login-settings__message">
<UpgradeMessage />
</div>
)}
</div>
you need wrapper in react component content like this 您需要像这样在react组件内容中包装
<div className="bx--col-md-12 bx--offset-xxl-1 bx--col-xxl-10">
{softlayerAccountId ? (
<div>
<TableToolbarComp />
{renderComponents()}
</div>
) : (
<div className="login-settings__message">
<UpgradeMessage />
</div>
)}
</div>
you always need return one element 你总是需要返回一个元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.