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