【问题标题】:Why does React.js axios data show in console but not on the screen?React.js axios 数据显示在控制台但未显示在屏幕上(尚未回答)
【发布时间】:2021-03-12 01:45:13
【问题描述】:

我是 React 的新手,我不知道我的代码出了什么问题。我正在使用https://swapi.dev/api/films/ 制作一个 React Axios 项目,字符数据显示另一个 API 链接,因此我制作了一个 Axios 循环来显示字符名称。字符名称可以显示在控制台中,所以应该成功但名称没有显示在屏幕上。已返回数据但仍未显示。我不知道出了什么问题,请帮助我。这是网络应用图片https://imgur.com/a/jQkuUI3,这是完整代码。谢谢!!

import React, { Component } from "react";
import axios from "axios";


class MovieDetail extends Component {
  state = {
    showMovieDetail: false,
  };

  movieListClicked = () => {
    this.setState({ showMovieDetail: !this.state.showMovieDetail });
  };

  getCharactersDetails = () => {
    const charsAPILink = this.props.characters;

    const charData = return charsAPILink.map((data) => {
      axios.get(data).then((response) => {
        console.log(response.data.name);
        return response.data.name;
      });
    });

    return charData;

  };

  render() {
    return (
      <div className="MovieDetail" onClick={this.movieListClicked}>
        {!this.state.showMovieDetail ? (
          <h2>{this.props.title}</h2>
        ) : (
          <div>
            <h3>
              Title : {this.props.title} <br />
              Episode : {this.props.episode} <br />
              {this.props.opening} <br />
              Director : {this.props.director} <br />
              Producer : {this.props.producer} <br />
              Date : {this.props.date} <br />
              Characters : {this.getCharactersDetails()}
            </h3>
          </div>
        )}
      </div>
    );
  }
}

export default MovieDetail;

【问题讨论】:

    标签: javascript reactjs axios


    【解决方案1】:

    您正在尝试在渲染函数中写入异步数据,因此它不会显示。

    您可以在状态中保存数据并更新状态。

    import React, { Component } from "react";
    import axios from "axios";
    
    
    class MovieDetail extends Component {
      state = {
        showMovieDetail: false,
        charData: []
      };
    
      movieListClicked = () => {
        this.setState({ showMovieDetail: !this.state.showMovieDetail });
      };
    
     componentDidMount() {
       this.getCharactersDetails()
    }
    
      getCharactersDetails = () => {
        const charsAPILink = this.props.characters;
        const temp = []
        
        charsAPILink.map((data) => {
          temp.push(axios.get(data))
        });
        
      promise.all(temp)
       .then(res => res)
        .then(responses => promise.all(responses.map(r => r.json()))
          .then(data => this.setState({charData: data}))
      };
    
      render() {
        return (
          <div className="MovieDetail" onClick={this.movieListClicked}>
            {!this.state.showMovieDetail ? (
              <h2>{this.props.title}</h2>
            ) : (
              <div>
                <h3>
                  Title : {this.props.title} <br />
                  Episode : {this.props.episode} <br />
                  {this.props.opening} <br />
                  Director : {this.props.director} <br />
                  Producer : {this.props.producer} <br />
                  Date : {this.props.date} <br />
                  Characters : {charData.map(item => {
                     return (<span> { item.name } </span>)
                  }}
                </h3>
              </div>
            )}
          </div>
        );
      }
    }
    
    export default MovieDetail;
    

    【讨论】:

    • 您好,感谢您的回复,但您的承诺部分的代码有一点错误imgur.com/a/Fp23CPt我不知道如何解决它
    • 抱歉回复晚了,这里好像还有2个错误imgur.com/a/e6h82E2
    【解决方案2】:

    您没有将 axios 响应返回给 map 函数,最好使用 async await 等到 api 响应然后返回。试试这个:-

    getCharactersDetails = async () => { const charsAPILink = this.props.characters;

    const charData = await getChars(charsAPILink )
    return charData 
    

    };

    async function getChars(charsAPILink) {
      let allChars = [];
      for (let i = 0; i < charsAPILink.length; i++) {
        await axios.get(charsAPILink[i]).then((response) => {
          allChars.push(response.data.title);
        });
      }
      return allChars;
    }
    

    【讨论】:

    • 您好,感谢您的回复,尝试了您的方法,但现在屏幕没有显示任何内容,并且显示此错误imgur.com/a/JxwjLBR 但名称仍显示在控制台中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2020-11-11
    • 1970-01-01
    • 2017-09-29
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多