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?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Ayaan Momin
  • 101
  • 2
  • 11
  • `console.log` returns `undefined`, so I don't really get what you're trying to do here? – Emile Bergeron May 14 '20 at 20:15
  • 1
    For the progressive loading, there's a [`LoadMinimal` example](https://github.com/leecade/react-native-swiper/blob/master/examples/components/LoadMinimal/index.tsx) in the _React-Native-Swiper_ documentation. – Emile Bergeron May 14 '20 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. – Ayaan Momin May 14 '20 at 20:21
  • Thanks Emile, Ill look into that! – Ayaan Momin May 14 '20 at 20:22

1 Answers1

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.

nipuna-g
  • 6,252
  • 3
  • 31
  • 48