1

I am creating an Github issue viewer with React.

I have a component that sets the repo, then I want to create separate components to get the issue name, number, login etc. These components will ultimately be used in the main component/view. I'm a bit stuck, below is what I have so far.

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
3
  • What's your question? Where are you stuck? Commented Jan 27, 2016 at 20:10
  • Possible duplicate of How can I prevent a component from rendering before data is loaded? Commented Jan 27, 2016 at 20:29
  • It's not an exact duplicate as-asked, but I think this type of problem is most readily solved with the container component solution. Commented Jan 27, 2016 at 20:29

1 Answer 1

1

Certainly not the only way to do it, but the following should work:

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'));
Sign up to request clarification or add additional context in comments.

3 Comments

I think you should call the getRepo method somewhere, possibly in componentDidMount.
@pawel Yes. Thanks :)
Thanks, this is helpful. However, it outputs this.props.repo.title and this.props.repo.number as text. Seems like I need to parse the JSON data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.