首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从api中获取数据并设置为图像滑块?

如何从api中获取数据并设置为图像滑块?
EN

Stack Overflow用户
提问于 2019-12-04 11:51:19
回答 3查看 654关注 0票数 0

我是新来的反应,我正在努力使形象粉碎机。我试图从api中获取数据,并将其设置为imageslider。我必须从api中获取图像标记,并将其设置为imageslider.My api,如下所示:

代码语言:javascript
复制
[ 
 { 
   "title":"Taylor Swift",
    "artist":"Taylor Swift",
    "url":"https://www.amazon.com/Taylor-Swift/dp/B0014I4KH6",
   "image":"https://images-na.ssl-images-amazon.com/images/I/61McsadO1OL.jpg",
   "thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg"
},
{ 
   "title":"Fearless",
   "artist":"Taylor Swift",
   "url":"https://www.amazon.com/Fearless-Enhanced-Taylor-Swift/dp/B001EYGOEM",
   "image":"https://images-na.ssl-images-amazon.com/images/I/51qmhXWZBxL.jpg",
   "thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg"
 }
 ]

我无法从api中获取图像数据并在呈现函数中呈现它。我所执行的措施如下:

代码语言:javascript
复制
export default class ViewpagerP extends Component {

constructor(props) {
    super(props);
    this.state = { isLoading: true, images: [], data: [] }
}
componentDidMount() {
    axios.get('https://rallycoding.herokuapp.com/api/music_albums')
        .then(response =>response.json())
        .then((data)=>{for (let index = 0; index < data.length; index++) {
            this.setState({
                isLoading: false,
                data: data[index]
            }) 
        }})    
}
render() {
    const { images } = this.state;
    return (
        <View style={styles.container}>
            <ImageSlider style={styles.viewPagerStyle}
                loopBothSides
                autoPlayWithInterval={1000}
                images={images}
                customSlide={({ index, item, style, width }) => (
                    <View key={index} style={style}>
                        <Image source={{ uri: item }} style={styles.customImage} />
                    </View>

                )}
            />
        </View>
    )
}
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-12-05 08:14:20

我解决了

代码语言:javascript
复制
 componentDidMount() {
    axios.get('https://rallycoding.herokuapp.com/api/music_albums')
        .then((response => this.setState({
            isLoading: false,
            data: response.data,
        }))
        )
      }

并在渲染中

代码语言:javascript
复制
  if (this.state.isLoading) {
            return (
                <View style={{ flex: 1, padding: 20 }}>
                    <ActivityIndicator />    
                </View>
            )
        }
        console.log(this.state.data)
    return (
        <View style={styles.container}>
            <ImageSlider style={styles.viewPagerStyle}
                loopBothSides
                autoPlayWithInterval={6000}
                images={this.state.data}
                customSlide={({ index, item, style, width }) => (
                    <View key={index} style={[style]}>
                        <Image source={{ uri: item.image }} style={styles.customImage} />
                    </View>

                )}
            />
        </View>
    );
}
票数 0
EN

Stack Overflow用户

发布于 2019-12-04 12:01:52

使用下面的代码,它将工作,因为您忘记添加数据状态到您的图像滑块库,这就是为什么您没有得到您想要的结果。

代码语言:javascript
复制
export default class ViewpagerP extends Component {

  constructor(props) {
    super(props);
    this.state = { isLoading: true, images: [], data: [] }
  }
  componentDidMount() {
    axios.get('https://rallycoding.herokuapp.com/api/music_albums')
      .then(response => response.json())

      .then((responseJson) => {
        this.setState({
            isLoading: false,
            data: responseJson
        })
      })

  }
  render() {
    return (
      <View style={styles.container}>
        <ImageSlider style={styles.viewPagerStyle}
          loopBothSides
          autoPlayWithInterval={1000}
          images={this.state.data}
          customSlide={({ index, item, style, width }) => (
            <View key={index} style={style}>
              <Image source={{ uri: item.image }} style={styles.customImage} />
            </View>

          )}
        />
      </View>
    );
  }
}
票数 0
EN

Stack Overflow用户

发布于 2019-12-04 12:50:26

由于您已经拥有状态的images,所以只使用它。

代码语言:javascript
复制
.then((data)=>{
        this.setState({
            isLoading: false,
            images:data
        }) 
    })  

现在您在this.state.images中有了响应,所以只需要在customSlide中做一个更改

代码语言:javascript
复制
customSlide={({ index, item, style, width }) => (
            <View key={index} style={style}>
                <Image source={{ uri: item.image }} style={styles.customImage} />
            </View>
)}

item将为您提供从该元素中提取image所需的整个节点,因此我们将编写item.image

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59175385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档