Reactjs 如何在React中的map函数中调用三元运算符内部的函数?
我想在React中调用map函数中的三元运算符,但遇到了错误 意外令牌,应为“…”Reactjs 如何在React中的map函数中调用三元运算符内部的函数?,reactjs,Reactjs,我想在React中调用map函数中的三元运算符,但遇到了错误 意外令牌,应为“…” {[‘我的个人资料’、‘探索更多’、‘注销’].map((文本、索引)=>{ 返回( {index==0&&} {index==1&&} {index==2&&} ) })} 以上是我尝试过的代码,请帮助我。试试这个符号: <List> {[ 'My Profile','Explore More', 'Logout'].map((text, index) => { return
{[‘我的个人资料’、‘探索更多’、‘注销’].map((文本、索引)=>{
返回(
{index==0&&}
{index==1&&}
{index==2&&}
)
})}
以上是我尝试过的代码,请帮助我。试试这个符号:
<List>
{[ 'My Profile','Explore More', 'Logout'].map((text, index) => {
return (
<ListItem button key={text}>
<ListItemIcon>
{index === 0 && <PersonIcon /> }
{index === 1 && <ExploreIcon /> }
{index === 2 && <ExitToAppOutlinedIcon /> }
</ListItemIcon>
<ListItemText
onClick={index === 2? logout: undefined}
primary={text}
/>
</ListItem>
)
})}
</List>
{[‘我的个人资料’、‘探索更多’、‘注销’].map((文本、索引)=>{
返回(
{index==0&&}
{index==1&&}
{index==2&&}
)
})}
您发布的代码中没有三元运算符此处共享的代码中没有三元/条件运算符。我建议只使用普通的if/else
或switch
结构。三元运算符是带有问号和冒号的表达式,类似于:isEven(x)?'偶数“:“奇数”
。我在你的代码片段中没有看到任何这些。{index==2&&(onClick={logout})是我代码第11行中的三元:)@TKharaishvili是一个三元的,如果不是条件的话……不是也叫三元的吗?它很有效,谢谢你救了我这么多:)我很高兴,我很感谢你选择我的答案我试过了,但它说我的名声不好,无论如何
<List>
{[ 'My Profile','Explore More', 'Logout'].map((text, index) => {
return (
<ListItem button key={text}>
<ListItemIcon>
{index === 0 && <PersonIcon /> }
{index === 1 && <ExploreIcon /> }
{index === 2 && <ExitToAppOutlinedIcon /> }
</ListItemIcon>
<ListItemText
onClick={index === 2? logout: undefined}
primary={text}
/>
</ListItem>
)
})}
</List>