简体   繁体   English

在返回方法之外的变量中存储 JSX 语句的最佳实践

[英]Best Practices to store JSX statement in a variable outside return method

I am trying to render different jsx for different conditions, What is best practice to store jsx in A variable.我正在尝试为不同的条件呈现不同的 jsx,将 jsx 存储在 A 变量中的最佳实践是什么。


  let jobb = jobs.map((job) => <JobListing key={job.id} job={job} />

  let spin = <Spin style={{ display: "block", margin: "0 auto" }} size="large" />

  let errors = (openNotificationWithIcon('error')),
  Spin style={{ display: "block", margin: "0 auto" }} size="large" />

  function rendering () {
    if(loading == false && error == false)
      return jobb
    if(loading == true && error == false)
      return spin
    if(loading == true && error == true)
    return errors
  }

  return (
    <Layout.Content style={{ padding: "0 50px" }}>
      <Row>
        {rendering()}
      </Row>
    </Layout.Content>
  );
}

i prefer to return it inside the function directly if it is not too large:如果它不是太大,我更喜欢直接在函数内部返回它:

  function rendering () {
    if(loading == false && error == false)
      return jobs.map((job) => <JobListing key={job.id} job={job} />
    if(loading == true && error == false)
      return <Spin style={{ display: "block", margin: "0 auto" }} size="large" />
    ...rest
  }

  return (
    <Layout.Content style={{ padding: "0 50px" }}>
      <Row>
        {rendering()}
      </Row>
    </Layout.Content>
  );
}

It is cleaner, but makes no difference in the performance.它更干净,但对性能没有影响。

If the return Components are multiline, you could create components/files and return them instead:如果返回组件是多行的,您可以创建组件/文件并返回它们:

  import Jobs from './Jobs'
  import CustomSpin from './CustomSpin'

  function rendering () {
    if(loading == false && error == false)
      return <Jobs />
    if(loading == true && error == false)
      return <CustomSpin />
    ...rest
  }

  return (
    <Layout.Content style={{ padding: "0 50px" }}>
      <Row>
        {rendering()}
      </Row>
    </Layout.Content>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java脚本承诺-如果语句返回最佳实践 - Java Script Promises - If Statement Return Best Practices 如何在语句外的if语句变量内调用return - how to call in return inside of if statement's variable outside of statement 最佳做法-多个退货声明 - Best Practices - Multiple Return Statements 将变量附加到XMLHttpRequest的最佳实践? - Best practices for attaching a variable to XMLHttpRequest? 如何返回对象jsx中的变量 - how to return a variable in an object jsx 将 jsx 标签作为返回语句返回时出错:Object 不支持属性或方法 - error in returning jsx tags as a return statement : Object doesn't support property or method 实施模板商店的最佳实践是什么? - What are the best practices for implementing the stencil store? VueX 商店中的 VueJS 错误处理最佳实践 - VueJS Error handling in VueX store best practices 将JSX从组件方法返回到渲染方法 - Return JSX from component method to render method REACT,有一个变量可以滚动 1-10 并在 JSX 的 return 语句中连接该变量。 数字在屏幕上呈现之前滚动 2 倍 - REACT, have a variable that rolls a num 1-10 and concatenating that variable in return statement in JSX. Number is rolled 2x before rendered on screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM