简体   繁体   English

React Component未呈现新列表

[英]React Component not rendering new list

Ok, so I'm building a giphy search project app just to get familiar with react and I have everything working so far except the part where I change the state of the input to be the value of the text input in the SearchBar component on the click of the button. 好的,所以我正在构建一个giphy搜索项目应用程序,只是为了熟悉react,到目前为止,我可以进行所有工作,除了将输入状态更改为SearchBar组件中文本输入值的部分。单击该按钮。 Right now I have it hard coded as duke b4 the button click and unc after the click but its not re rendering the list of gifs. 现在,我已经将其硬编码为duke b4按钮,单击后单击unc,但是它没有重新呈现gif列表。 Any help and explanations woulds be appreciate. 任何帮助和解释将不胜感激。

import React, { Component } from 'react';
import axios from "axios";
import styles from './App.css';

import Header from './Components/Header/Header';
import GifList from './Components/GifList/GifList';
import SearchBar from './Components/SearchBar/SearchBar'


class App extends Component {

  state = {
    title: "Giphy Search App",
    gifs: [],
    userInput: "duke"
  }

  componentDidMount() {
      axios.get(`http://api.giphy.com/v1/gifs/search?q=${this.state.userInput}&limit=20&api_key=ms344CewNH5NEbybHwQifMZImoQfEQ38`)
        .then((res) => {
          const arr = res.data.data;
          this.setState({ gifs: arr });
      });
  }

  searchQuery = () => {
      this.setState({ userInput: "unc" });
  }

  render() { 
    return (
      <div className={styles.app}>
        <Header title={this.state.title}/>
        <SearchBar query={this.searchQuery}/>
        {this.state.gifs.length > 0 && <GifList gifList={this.state.gifs}/>}
      </div>
    );
  }
}

export default App

Array is mutable. 数组是可变的。 Change your setState to 将您的setState更改为

this.setState({ gifs: [...arr] });

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

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