javascript - React 无法显示 json - 数据已分配值但从未使用

标签 javascript reactjs

我只使用 React 两天,并且一直在显示 JSON 文件的获取结果。当我执行 console.log 时,数据出现在控制台中,但我无法弄清楚如何通过渲染函数将其显示回来。

此外,我不知道如何传递或设置变量的值。在这种情况下,我收到警告 - “数据已分配值但从未使用”

我尝试过 { JSON.stringify(this.Songs, null, 2) }。 我尝试调用 this.data[0].title 来仅显示 JSON 中的第一个标题,但我收到一条错误,指出 0 未定义。

这就像 this.data 或 this.Songs 的值在其原始功能之外丢失了。

我尝试过使用let data、const data、this.data = this.data.bind等...

如何使这些值可用于 render()? 如何将它们呈现到页面?

这是代码并提前致谢:

import React from 'react';

class Songs extends React.Component {

constructor(){
    super();
    const data = [];
}

componentDidMount() {
    fetch('http://localhost:3000/data.json')
    .then((response) => response.json())
    .then((responseJson) => {
        this.Songs = responseJson;
        this.data = responseJson;
        this.getData(this.Songs);
        this.createColumns();
    })
    .catch((error) => {
        console.error(error);
    });
}

createColumns() {
    this._columns = ['id', 'title', 'original_band', 'description', 'date_posted', 'download_midi_tabs', 'youtube_link', 'download_guitar_m4v' ];
    console.log('inside createColumns: ' + this._columns);
}

getData(data){
    console.log('inside getData');
    console.log(data);
}

render() {
    return (
        <div>
            { JSON.stringify(this.data, null, 2) }
        </div>
    );
 }
}

export default Songs;

以下是 JSON 中的两行示例:

[ { "id": "1", "title": "2 minutes 2 midnight", "original_band": "Iron Maiden", "description": "", "date_posted": "2012-02-06 17:14:56", "download_midi_tabs": "http://kronusproductions.com/songs_angular/assets/downloads/2_minutes_to_midnight.zip", "youtube_link": "http://youtu.be/VAm3-vyfJwI", "download_guitar_m4v": "http://kronusproductions.com/uploads/serious/2-minutes-to-midnight-vst.mp3", "download_enabled": "1" }, { "id": "2", "title": "909", "original_band": "The Beatles", "description": "Even though this song would be recorded at the end of the Beatles time together, it was one of the first songs that Lennon and McCartney wrote together. I have added horns in the place of the guitar licks that Lennon recorded.", "date_posted": "2012-02-06 17:14:56", "download_midi_tabs": "http://kronusproductions.com/songs_angular/assets/downloads/909.zip", "youtube_link": "https://youtu.be/DxuEYgbUtqE", "download_guitar_m4v": "http://kronusproductions.com/uploads/serious/909-vst.mp3", "download_enabled": "1" }

最佳答案

您必须使用setState来设置组件状态,并且该状态将驻留在组件的state中。

示例

class Songs extends React.Component {
  state = {
    data: []
  };

  componentDidMount() {
    fetch("http://localhost:3000/data.json")
      .then(response => response.json())
      .then(responseJson => {
        this.setState({ data: responseJson });
      })
      .catch(error => {
        console.error(error);
      });
  }

  render() {
    return (
      <div>
        {this.state.data.map(obj => {
          return (
            <div>
              <h2>{obj.title}</h2>
              <h3>{obj.original_band}</h3>
              <p>{obj.description}</p>
            </div>
          );
        })}
      </div>
    );
  }
}

关于javascript - React 无法显示 json - 数据已分配值但从未使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51107076/

相关文章:

reactjs - 如何将 ReactJS 组件与 Clojurescipt/Reagent 结合使用

javascript - 有什么方法可以将函数的范围限定到 Meteor 中的一组不同模板吗?

javascript - 在 jquery 中切换按钮文本

javascript - 使用最佳实践 React 导入模块

reactjs - ESLint 首选默认导出导入/prefer-default-export

reactjs - 如何使用 jest 模拟类和命名空间枚举?

reactjs - 每次有人使用 firebase 和 Reactjs 访问页面时计算浏览次数

javascript - key 未定义 React

javascript - 如何使同位素画廊拉伸(stretch)周围的容器?

javascript - 使用 javascript 媒体查询应用样式