javascriptarraysreactjsreact-nativereact-native-snap-carousel

How to provide API data to react-native-snap-carousel


i am using react-native-snap-carousel for my first react-native app.

I have read docs but i see it use static data. But i want to get data form server with API, how can i do it?

Thank you for helping.


Solution

  • You can fetch data from the server and pass it like so:

    class MyCoponent extends React.Component{
    
       state = { data: null }
    
       fetchData = () => {
          // const result = fetch from api ... 
          this.setState(() => ({ data: result }))
       }
    
       render(){
          const { data } = this.state
          if(!data){
             return <Text>No data</Text>
          }
          return(
             <Carousel data={data}/>
          )
       }
    
    }