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
Add a ment  | 

2 Answers 2

Reset to default 9

Couple 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