0

How to use flex in react-native to make any element inside <View></View> to arrange themselves when their length got bigger than their container's ?

Below is my code:

const FilmCasts = ({ artists }) => (
<View style={{ flex: 1, flexDirection: 'row', }}>
   {artists.map(artist => (
     <Image
       key={cast.name}
       source={{ uri: artist.img }}
       style={{ width: 80, height: 100 }}
    />
   ))}

</View>
);

This is what I want. I want to make the 5th and others to go below the first row automatically after they hit the screen width limit, like what HTML usually does

This is what I want

But instead, this is what I get when in React Native, the <Image> elements continue to be rendered outside their parent's width

This is what I get

3
  • try flexWrap:'wrap' Commented Jul 31, 2018 at 11:00
  • @AravindS works for me! Thank you!!!! Commented Jul 31, 2018 at 11:05
  • Awesome ! i will add it as answer Commented Jul 31, 2018 at 11:05

2 Answers 2

2

You can use the prop flexWrap:'wrap' for wrapping the elements. flexWrap controls whether children can wrap around after they hit the end of a flex container. More here

const FilmCasts = ({ artists }) => (
<View style={{ flex: 1, flexDirection: 'row',flexWrap:'wrap' }}>
   {artists.map(artist => (
     <Image
       key={cast.name}
       source={{ uri: artist.img }}
       style={{ width: 80, height: 100 }}
    />
   ))}

</View>
);
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply defined image with dynamically using the Dimension property of react native .

like when you are using

<Image
   resizeMode="cover" or try "contain"
   key={cast.name}
   source={{ uri: artist.img }}
   style={{ width: width * 0.5 , height: 100 }} // this will be done currently defining the values . 
/>

Here is one blog regarding image handling in react native Click to see the blog

Hope my answer will give an idea to slove your problem

2 Comments

even though flexWrap property is the easiest way, but I think I will try your suggestion here, I want to make sure same UI experience across any device and display width
yes , both approach will work with width concepts . thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.