admin管理员组文章数量:1291013
Problem
I am using a flatlist in react native, and want to pare to variables in the flatlist, and render a text ponent if the two variables are equal, but render nothing if they are not. I've tried many methods of doing this, but they nothing has worked. I would love some help figuring out a way to do this! Thank you.
Problem
I am using a flatlist in react native, and want to pare to variables in the flatlist, and render a text ponent if the two variables are equal, but render nothing if they are not. I've tried many methods of doing this, but they nothing has worked. I would love some help figuring out a way to do this! Thank you.
Share Improve this question asked Dec 1, 2017 at 3:59 user8749566user8749566 1- Can you please give some detail information or something what you have done in your code? – JAINESH DOSHI Commented Dec 1, 2017 at 4:05
2 Answers
Reset to default 9Couple ways that spring to mind straight away. Ill just assume what you are trying to pare but you can switch these variables out for whatever you please. First thing you could do is have your text be conditional inside of your Text
ponent, EG
<Text>{this.state.variable == this.props.stuff ? "RENDER TEXT" : ""}</Text>
or, if you want to emit the Text
ponent when variables are not equal, have a function inside of your class that will return the Text
ponent conditionally
renderMessage = () => {
if(this.state.variable == "stuff"){
return(
<Text>They were equal</Text>
);
}else{
return(
<View></View> // OR WHATEVER YOU WANT HERE
);
}
}
...
//Then in your render function
....
<View style = {styles.whatever}>
{this.renderMessage()}
</View>
just pare your data in renderItem method accordingly
<FlatList
data={this.props.data}
renderItem={this._renderItem}
/>
_renderItem = ({item}) => (
if(item=='somthing'){
return <Text> your test </Text>
}else{
return <Text>some other text</Text>
}
);
if you want to pare your text in ponent then
<View>
{
item.data == 'somthing'
?
<Text>if</Text>
:
<Text>else</Text>
}
</View>
本文标签: javascriptHow to render something in an if statement React NativeStack Overflow
版权声明:本文标题:javascript - How to render something in an if statement React Native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504312a2382228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论