javascript - 在多个react.js组件中渲染json数据

标签 javascript json reactjs

我想从 json 文件中获取一些值并将它们呈现在多个组件中。这段代码似乎不起作用。请提出任何更改建议。范围可能存在一些问题。

var App = React.createClass({

    getInitialState: function(){
      return { myData: [] }

    },
    showResults: function(response){
        this.setState(
          {myData: response}
          )
    },

    loadData: function(URL) {
      $.ajax({
        type: 'GET',
        dataType: 'jsonp',
        url: URL,
        success: function(response){
          this.showResults(response)
        }.bind(this)
      })
    },

    render: function(){
      this.loadData("fileLocation/sample.json");
      return(
        <div>
        {myData.key1}
        <Component1 value={myData.key2} />
        <Component2 value={myData.array[0].key3}/>
        </div>
      )
    }
  });

  var Component1 = React.createClass({
    render: function () {
      return(
        <div>{this.props.value}</div>
      )
    }
  });

  var Component2 = React.createClass({
    render: function () {
      return(
        <div>{this.props.value}</div>
      )
    }
  });
  ReactDOM.render(<App/>, document.getElementById('content'));

这是我试图从中获取的sample.json 文件。即使这也显示语法错误

{
  key1:"value1",
  key2:"value2",
  array: [
    {key3:"value3"},
    {key4:"value4"}
  ]
}

最佳答案

loadData 处正确调用 showResults [1] :

var App = React.createClass({

    getInitialState: function(){
      return { myData: [] };
    },

    showResults: function(response){
        this.setState({
            myData: response
        });
    },

    loadData: function(URL) {
      var that = this;
      $.ajax({
        type: 'GET',
        dataType: 'json',
        url: URL,
        success: function(response){
          that.showResults(response);
        }
      })
    },

loadDatarender 移动到 componentDidMount [2] ,并正确访问 myData [3] :

    componentDidMount: function() {
      this.loadData("fileLocation/sample.json");
    },

    render: function(){
      return(
        <div>
        {this.state.myData.key1}
        <Component1 value={this.state.myData.key2} />
        <Component2 value={this.state.myData.array[0].key3}/>
        </div>
      )
    }
});

保持 Component1Component2 不变:

var Component1 = React.createClass({
    render: function () {
      return(
        <div>{this.props.value}</div>
      )
    }
});

var Component2 = React.createClass({
    render: function () {
      return(
        <div>{this.props.value}</div>
      )
    }
});
ReactDOM.render(<App/>, document.getElementById('content'));

关于javascript - 在多个react.js组件中渲染json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983158/

相关文章:

c# - 如何正确反序列化和迭代 JSON 对象数组

javascript - 想使用 React Js 创建拖放组件

javascript - 在 contenteditable div 中用 html 替换某些最后输入的单词

javascript - 根据 session ID 创建对象变量

javascript - 通过 ID 从数组中获取特定 JSON 对象并返回 JSON 对象 (Javascript/JQuery)

html - 如何使用 Bootstrap(和 React)居中图像

reactjs - 如何使用 Redux 表单向导以及每页永久 URL

javascript - Rails 保存动态创建的字段时不允许的参数

javascript - 从谷歌街景中的原始全景图向北行驶

python - 将 Ruby 中的 json 响应移植到 Python