【发布时间】:2019-02-17 03:43:43
【问题描述】:
<Container>
<Header function1={()=>this.props.navigation.openDrawer()}/>
<Content>
{this.state.formInputs.formFeilds.map((data)=>(
{this.state[`${data}`]?<Item floatingLabel success>:<Item floatingLabel>}
<Label>data</Label>
<Input value={data}/>
<Icon name='checkmark-circle' />
</Item>
))}
</Content>
</Container>
)
我的 react native 类的渲染函数中有这个简单的返回语句
我想使用 <Item floatingLabel success> 当这个 this.state[${data}] 是true else <Item floatingLabel > 当它为 false 时。
所以我使用三元运算符(?:)但代码抛出错误
这是保留关键字
所以我将{this.state[${data}]?<Item floatingLabel success>:<Item floatingLabel>} 转换为this.state[${data}]?<Item floatingLabel success>:<Item floatingLabel>
现在错误是
> Unexpected token, expected , (128:5) 126 | ))} 127 | 128 | </Content> 129 | ^ </Container>
但是如果我像这样渲染
<Container>
<Header function1={()=>this.props.navigation.openDrawer()}/>
<Content>
{this.state.formInputs.formFeilds.map((data)=>(
<Item floatingLabel success>
<Label>data</Label>
<Input value={data}/>
<Icon name='checkmark-circle' />
</Item>
))}
</Content>
然后代码运行成功。但我需要使用三元运算符。 请帮忙。
【问题讨论】:
标签: react-native jsx