Skip to content

Instantly share code, notes, and snippets.

@xie-qianyue
Created March 30, 2016 12:24
Show Gist options
  • Save xie-qianyue/b446997dee3351dc6a82b5849fec27ef to your computer and use it in GitHub Desktop.
Save xie-qianyue/b446997dee3351dc6a82b5849fec27ef to your computer and use it in GitHub Desktop.
React container component
class TodoContainer extends Component {
componentDidMount() {
this.setState({ isLoding: true });
fetch('/todos.json').then(
todos => {
this.setState({
error: null,
todos,
isLoding: false
});
},
error => this.setState({ error })
);
}
render() {
return <Todo {...this.state} />
}
}
class Todo extends Component {
render() {
const { error, isLoding, todos } = this.props;
if(error) {
return <ErrorPage />;
}
if(isLoding) {
return <Spinner />;
}
if(!todos.length) {
return <EmptyResult />;
}
const lists = todos.map(todo => {
return <TodoItem {...todo} />;
});
return <ul> {lists} </ul>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment