简体   繁体   English

如何在条件中另一个组件下方的无状态组件上呈现函数?

[英]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.

相关问题 如何通过在无状态组件中的开关内调用函数来更新父状态? - How can I update the parent state by calling a function within a switch in my stateless component ? 如何在reactjs中调用无状态功能组件内部的函数 - How can I call a function inside of stateless functional component in reactjs 如何在本机反应中将无状态 function 转换为 class 组件 - How can I convert stateless function to class component in react native 在React上,如何在功能/无状态组件上调用组件安装上的函数? - On React, how can I call a function on component mount on a functional/stateless component? ReactJS,如何在另一个组件的render方法中渲染一个组件? - ReactJS, how to render a component within another component's render method? 在 ReactJS 中,如何在另一个组件中渲染一个组件? - In ReactJS, how do I render one component within another? 如何在无状态组件中访问 function? - How to access a function in a stateless component? 如何将 Class 组件转换为 Function 无状态组件? - How to transform a Class Component to a Function Stateless Component? React - 如何使用 OnClick 在 class 中渲染组件? - React - How can I render a component within a class by using OnClick? 为什么我可以调用在组件下方声明的 function? - How come I can invoke a function declared below a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM