0

So essentially, my problem stems from trying to solve this issue.

function SwiperComponent () {

  const [item, setItems] = useState([["hi",console.log("hi")], ["hello",console.log("hello")], ["never",console.log("never")], ["sorry",console.log("sorry")]])
  const swiperItems = item.map(ite => {
    return(
        <View style={styles.slide1} >
          <Text style={styles.text}>{ite[0] + ite[1]}</Text>
        </View>
    )
  })
    return (
        <Swiper
            key={item.length}
            style={styles.slide1}
        >
          {swiperItems}
        </Swiper>
    )

}

So my code is fairly simple, I am using the React-Native-Swiper library, to essentially make the views in the array swipable.

Now the problem is, when I run the code, it generates all the views in the array at the same time, I know this because i can see in the console, the print statements all being printed upon starting. The issue that arises with this, is what if I have a long array of lets say pictures, I dont want to retrieve all those images at once, since it will tank the performance, but also obviously there is a huge chance the user doesnt go through all the images, in which case, I will make calls to my server to retrieve the images unnecessarily (I am using firebase, so I am trying to limit this for cost issues).

So how can I render these images only when I get closer to them, after I start swiping?

4
  • console.log returns undefined, so I don't really get what you're trying to do here? Commented May 14, 2020 at 20:15
  • 1
    For the progressive loading, there's a LoadMinimal example in the React-Native-Swiper documentation. Commented May 14, 2020 at 20:19
  • 1
    Honestly, upon thinking more, this might just be a backend problem. Prolly nothing to do with react, Ill still leave this post up, just in case this can be solved on the front end, somehow. Sorry for my foolishness I am a beginner. Commented May 14, 2020 at 20:21
  • Thanks Emile, Ill look into that! Commented May 14, 2020 at 20:22

1 Answer 1

1

You should be able to use the loadMinimal setting in the react-native-swiper.

Here, you need to specify that loadMinimal is true and the number of slides you need to load before and after the current slide by setting loadMinimalSize.

<Swiper
  key={item.length}
  style={styles.slide1}
  loadMinimal={true} // only loads the current page + [loadMinimalSize] amount of pages before and after
  loadMinimalSize={0}
  loadMinimalLoader={() => (
    <View>
      <Text>Loading</Text>
    </View>
  )} // optional loader to show while page loads
>
  {swiperItems}
</Swiper>

Alternatively, you can use the lazy load image library that would only load images when you scroll within a threshold. https://github.com/Aljullu/react-lazy-load-image-component.

Sign up to request clarification or add additional context in comments.

Comments

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.