简体   繁体   English

SetState似乎没有更新

[英]SetState doesn't seem to update

I'm working on a very simple react-native app where I type the name of an artist in a searchbox, retrieve a list of artists from the spotify API and I display this list in FlatList component. 我正在开发一个非常简单的本地本机应用程序,在该应用程序中,我在搜索框中键入艺术家的名字,从spotify API中检索艺术家的列表,然后在FlatList组件中显示该列表。

I manage to get the list of artists and I want to save it in the local state so that I pass it to the FlatList component. 我设法获取艺术家列表,并希望将其保存在本地状态,以便将其传递给FlatList组件。

The list object looks like this : [{...}, {...}, {...}, {...}] 列表对象如下所示:[{...},{...},{...},{...}]

But it doesn't seem to work and I think that my state is not updating and I don't know what I'm doing wrong. 但这似乎不起作用,我认为我的状态没有更新,并且我不知道自己在做什么错。

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);

}

Thank you for your help. 谢谢您的帮助。

UPDATE 更新

I also tried using this without success : 我也尝试使用这个没有成功:

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

In the makeQuery function you need to set the response from the server like.. 在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM