我试图渲染一个条件,如果在我的构造函数的状态变量内。构造函数的this.state中的条件IF语句Reactjs
我原来的代码是这样的,它工作
constructor(props, context){
super(props, context);
}
this.state={
... // the rest of my state
columns: [{
render:
row => (
row.row.exported === 0 ?
<span className={styles.centered} >
{
row.value === true ? <img src={checkedImage} />
: <img src={uncheckedImage} />
}
</span>: ""
),
maxWidth: 60,
hideFilter: true,
style: {textAlign:"center"},
}]
}
,我希望做这样的事情:用
constructor(props, context){
super(props, context);
this.state={
... // the rest of my state
columns:
render:
if(this.state.profile.role == 'dba' || this.state.profile.role == 'both'){
row => (
row.row.exported === 0 ?
<span className={styles.centered} >
{
row.value === true ? <img src={checkedImage} />
: <img src={uncheckedImage} />
}
</span>: ""
}
),
maxWidth: 60,
hideFilter: true,
style: {textAlign:"center"},
}]
}
}
在我的渲染方法IM反应表所以它看起来像这样:
Render方法
render(){
return(
<ReactTable columns={this.state.columns} />
)
}
是否有可能或有任何变通来实现这一目标?
你为什么一直JSX状态......应该不是状态只保留纯数据? – wesley6j
你可以使用renderif进行条件渲染 – user2906608
@ wesley6j对于React来说,我会怎么做呢? – Emmanuel