Created
February 9, 2017 05:37
-
-
Save boertel/1bc0fee0189c969fa6748b911e69fe11 to your computer and use it in GitHub Desktop.
github react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Root extends React.Component { | |
render() { | |
const repos = this.props.response.data; | |
return ( | |
<div>{repos.map(function(repo) { return <p>{repo.name}</p>; })}</div> | |
); | |
} | |
} | |
function connect(url, component) { | |
class Wrap extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
response: undefined, | |
}; | |
} | |
componentDidMount() { | |
$.ajax({ | |
url: this.props.url, | |
method: 'GET', | |
dataType: 'jsonp', | |
success: (function(response) { | |
this.setState({ | |
response, | |
}); | |
}).bind(this) | |
}); | |
} | |
render() { | |
const { response } = this.state; | |
if (!response) { | |
return (<div>Loading...</div>); | |
} | |
const child = React.createElement(component, { response, }); | |
return ( | |
<div>{child}</div> | |
); | |
} | |
} | |
return (<Wrap url={url} />); | |
} | |
ReactDOM.render(connect('https://api.github.com/users/boertel/repos', Root), document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment