javascript - SetState 似乎没有更新

标签 javascript react-native

我正在开发一个非常简单的 react 原生应用程序,我在搜索框中输入艺术家的名字,从 Spotify API 检索艺术家列表,然后在 FlatList 组件中显示该列表。

我设法获取艺术家列表,我想将其保存在本地状态中,以便将其传递给 FlatList 组件。

列表对象如下所示:[{...}, {...}, {...}, {...}]

但它似乎不起作用,我认为我的状态没有更新,我不知道我做错了什么。

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  FlatList,
  StatusBar,
  TextInput,
} from 'react-native';

import colors from './utils/colors';
import { List, ListItem, SearchBar } from 'react-native-elements';
import { searchArtist } from './utils/fetcher';
import { debounce } from 'lodash';


export default class spotilist extends Component {
  constructor(props) {
    super(props);

    this.state = {
      loading: false,
      data: [],
      query: '',
      artists: [],
      error: null,
      refreshing: false,
    };
  }


  render() {
    return (

      <View style={ styles.container }>
        <StatusBar barStyle="light-content" />
        <TextInput style={ styles.searchBox }
          value={this.state.value}
          onChangeText={ this.makeQuery }
        />
        <List>
          <FlatList
            data={this.state.artists}
            //renderItem={({item}) => <Text>{item.name}</Text>}
          />
        </List>

        // {
        //   this.state.artists.map(artist => {
        //     return (
        //       <Text key={artist.id}>{artist.name}</Text>
        //     )
        //   })
        // }

      </View>
    );
  }

  makeQuery = debounce(query => {
    searchArtist(query)
      .then((artists) => {
        console.log(artists); // I have the list
        this.setState({
          artists: this.state.artists,
        });
      })
      .catch((error) => {
        throw error;
      });
  }, 400);

}

感谢您的帮助。

更新

我也尝试过使用这个但没有成功:

    <List>
      <FlatList
        data={this.state.artists}
        renderItem={({ item }) => (
          <ListItem
            roundAvatar
            title={item.name}
            avatar={{ uri: item.images[0].url }}
          />
        )}
      />
    </List>

最佳答案

在 makeQuery 函数中,您需要设置来自服务器的响应,例如..

makeQuery = debounce(query => {
searchArtist(query)
  .then((artists) => {
    console.log(artists); // I have the list
    this.setState({
      artists: artists,   //Here is the change
    });
  })
  .catch((error) => {
    throw error;
  });
}, 400);

关于javascript - SetState 似乎没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251904/

相关文章:

javascript - Ember.js (RC1) CollectionView.didInsertElement 触发得太早

javascript - 如果图像高度超过图像宽度,通过裁剪图像的顶部和底部来垂直居中图像 - 仅使用 CSS 可能吗?

javascript - MockJax 未在 JavaScript 应用程序中发送对我的 AJAX 请求的响应

react-native - React Native ,动画负旋转

node.js - 无法在我的 ReactNative 应用程序上安装 Realm;与 : npm ERR! 在 Realm @3.6.0 安装脚本失败 'node-pre-gyp install --fallback-to-build'

javascript - 使用 navigator.geolocation 获取地理位置

javascript - 仅直接移动设备而不是平板电脑

ios - React Native 创建使用 cocoapods 的库

ios - 在 iOS 上通过 React Native 登录 Facebook 时出现 "Exception throw"错误