【发布时间】:2020-01-09 01:25:58
【问题描述】:
我是 React Js 的新手。如果地图内的 ELSE 条件给了我错误。
我想将 IF ELSE 条件放在地图中。如果 IF ELSE 是可能的,我不想使用 三元运算符。
class Home extends Component {
constructor(props){
//codes
}
testFunction = () => {
//postDetails is something fetch from the server
let tableDetail;
tableDetail = Object.keys(postDetails).map((i) => (
let val;
/* I dont know how to achieve this part */
if(typeof postDetails[i].comment === 'string'){
val = <div>A String!</div>
}else{
val = <div>Not a string!</div>
}
/* I dont know how to achieve this part */
<>
{val}
// other codes ..
</>;
));
return tableDetail;
}
render() {
return (
<>
<h3>Table</h3>
{this.testFunction}
</>
)
}
【问题讨论】:
标签: reactjs if-statement object-object-mapping