gpt4 book ai didi

javascript - 如何将 json 值传递到 React 组件

转载 作者:行者123 更新时间:2023-11-27 23:24:49 26 4
gpt4 key购买 nike

我正在使用 React 创建一个 Github 问题查看器。

我有一个设置存储库的组件,然后我想创建单独的组件来获取问题名称、编号、登录名等。这些组件最终将在主组件/ View 中使用。我有点卡住了,下面是我到目前为止所拥有的。

var GetRepo = React.createClass({
getRepo: function(){
var issues = $.getJSON('https://api.github.com/repos/facebook/react/issues', function (data) {
})
},
render: function() {
return <div>My repo: {this.props.repo}</div>
}

});

ReactDOM.render(<GetRepo repo="facebook/react/issues" />, document.getElementById('main'));


var IssueName = React.createClass({
});
//IssueName gets the data.title (the issue name) using repo GetRepo

var IssueNumber = React.createClass({
});

//IssueNumber gets the data.number (the issue number) using repo from GetRepo

最佳答案

当然不是唯一的方法,但以下应该可行:

var GetRepo = React.createClass({

getInitialState: function() {
return {
repo: {}
};
},

componentDidMount: function(){
var that = this;
var issues = $.getJSON('https://api.github.com/repos/facebook/react/issues', function (data) {
that.setState({
repo: data
});
});
},

render: function() {
return (
<div>
<IssueName repo={this.state.repo} />
<IssueNumber repo={this.state.repo} />
</div>
);
}
});


//IssueName gets the data.title (the issue name) using repo GetRepo
var IssueName = React.createClass({
render: function() {
return (
<div>this.props.repo.title</div>
);
}
});

//IssueNumber gets the data.number (the issue number) using repo from GetRepo
var IssueNumber = React.createClass({
render: function() {
return (
<div>this.props.repo.number</div>
);
}
});

ReactDOM.render(<GetRepo repo="facebook/react/issues" />, document.getElementById('main'));

关于javascript - 如何将 json 值传递到 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com