在多个 react.js 组件中呈现 json 数据
Rendering json data in multiple react.js components
我想从 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
:
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);
}
})
},
将 loadData
从 render
移动到 componentDidMount
, and properly access myData
:
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>
)
}
});
保持 Component1
和 Component2
原样:
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'));
我想从 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
:
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);
}
})
},
将 loadData
从 render
移动到 componentDidMount
myData
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>
)
}
});
保持 Component1
和 Component2
原样:
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'));