Home > Software engineering >  React Native, creating a if loop inside a return
React Native, creating a if loop inside a return

Time:06-04

I want to create an if loop inside my Flatlist return so that each time a new Flatlist is called, my 'pass' will go back to 0 Here is my code:

               <FlatList
                    data={letter.description}
                    numColumns={2}
                    keyExtractor={(_, index) => index.toString()}
                    renderItem={({ item }) => {
                      return (
                        //if (pass>letter.description?.length) {
                        //pass=0;
                        //}
                        <View>
                          <Text
                            style={{
                              fontSize: 18,
                              fontWeight: "600",
                              marginTop: 25,
                              width: "50%",
                            }}
                          >
                            {letter.data[pass]}
                            {"\n"}
                          </Text>
                          <Text style={{ marginTop: 50, width: "50%" }}>
                            {letter.description[pass]}
                            {pass  }
                            {"\n"}
                          </Text>
                        </View>
                      );
                    }}
                  />

How do I do it?

CodePudding user response:

Slight nitpick... if is not a "loop". Those are two very different things. Having said that...

You're trying to put a statement inside of another statement. Just move the if to before the return:

if (pass>letter.description?.length) {
  pass=0;
}
return (
  //...
);

They're two separate operations, there's no reason to try to munge them together.

  • Related