var Movie = React.createClass({
getInitialState: function() {
$.ajax({
url: "getjsonarray.php",
dataType: 'json',
method: "POST",
data: {
startpoint: 0,
perpage: 2
},
success: function(data) {
this.setState({
json: data
}, function() {
}.bind(this));
}.bind(this),
});
return null;
},
render: function() {
return (
<div>
{
this.state.json.map(function(object, i){
return (
<div key={i}>
<h1>Movie {i}</h1>
<h2>Genre {i}</h2>
</div>
);
})
}
</div>
);
}
});
ReactDOM.render(<Movie/>, document.getElementById('container'));
ここでは、バックエンドからjsonアレイを反応js経由で配列を反復して取得しようとしています。しかし、私はこのエラーだけを受け取ります。
TypeError: this.state is null
ajaxレスポンス配列で値を返す方法はありますか?
これは、ここでの問題は、AjaxがgetInitialStateが最初に行くので、それはレンダリングに直進非同期であるということです、API呼び出しが輸送中にまだある、私はバックエンドから何を得る
[{"id":"1","image":"http:\/\/images.prd.mris.com\/image\/V2\/1\/Yu59d899Ocpyr_RnF0-8qNJX1oYibjwp9TiLy-bZvU9vRJ2iC1zSQgFwW-fTCs6tVkKrj99s7FFm5Ygwl88xIA.jpg","price":"$1,975,000 ","address":"609 W Gravers Ln","area":"4,820 SqFt","beds":"5","baths":"5","desc":"Situated between fairmount park and the prestigious philadelphia cricket club, this beautiful 2+ acre property is truly","subdesc":"Courtesy of HS Fox & Roach-Chestnut Hill Evergreen"},{"id":"2","image":"http:\/\/images.prd.mris.com\/image\/V2\/1\/vGoNjc2jHGb87GlnnDQlf6LxeOUgIOn0bL6Wvn1nEnig2Ntq6W7xN5cOQBZZeNxl9O42DOkHUw0LNnj1ZB2KHA.jpg","price":"$1,500,000","address":"1220-32 N Howard St","area":"4,900 SqFt","beds":"1","baths":"1","desc":"A once in a lifetime opportunity to own a unique live \/ work space in one of philadelphia's most popular neighborhoods.","subdesc":"Courtesy of ll Banker Preferred-Philadelphia"}]
感謝:) – vimuth