【发布时间】:2019-09-23 23:41:09
【问题描述】:
在我的普通 React 类组件中,我在 render() 方法中做了一些检查,然后返回条件 html 渲染。现在,我正在使用一个反应功能组件,它显然没有 render() 方法......我将如何在这里进行条件检查?只是在普通函数中,然后从这些函数中返回 html 代码?
例如类组件:
render() {
let test;
if (this.state.test ===true) {
test = (
<p>This is a test</p>
)
}
return(
{test}
)
}
在功能组件中? :
return (
<p >
{checkIcon()} //normal Javascript functions?
</p>
)
【问题讨论】:
-
你试过了吗?有什么错误吗?使用普通的 JS 函数完全没问题。
-
您需要将
test作为参数传递。其余的几乎保持不变。 -
文档为您提供了一个带有功能组件的示例https://reactjs.org/docs/conditional-rendering.html
-
{checkIcon() && <p>This is a test</p>}
标签: javascript reactjs function