Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Containers - South Bay JS

Eli
February 10, 2016

React Containers - South Bay JS

Lightning talk about using "containers" in reactjs to create reuseable components.

Eli

February 10, 2016
Tweet

More Decks by Eli

Other Decks in Technology

Transcript

  1. CommentList Component class CommentList extends React.Component { constructor() { super();

    this.state = {comments: [], title: ''}; } componentDidMount() { $.post('/echo/json/', {json: JSON.stringify(jsonData), delay: 1}) .done(d => this.setState({comments: d['comments'], title: d['title']})); } render() { let comments = this.state.comments.map(c => (<li>{c.body} - <em>{c.author} </em> </li>)); return ( <div> <h2> {this.state.title} Comments </h2> <ul> {comments} </ul> </div> ); } } React.render(<CommentList />, document.getElementById('test'));
  2. CommentList Component class CommentList extends React.Component { constructor() { super();

    this.state = {comments: [], title: ''}; } componentDidMount() { $.post('/echo/json/', {json: JSON.stringify(jsonData), delay: 1}) .done(d => this.setState({comments: d['comments'], title: d['title']})); } render() { let comments = this.state.comments.map(c => (<li>{c.body} - <em>{c.author} </em> </li>)); return ( <div> <h2> {this.state.title} Comments </h2> <ul> {comments} </ul> </div> ); } } React.render(<CommentList />, document.getElementById('test'));
  3. CommentList Component class CommentList extends React.Component { constructor() { super();

    this.state = {comments: [], title: ''}; } componentDidMount() { $.post('/echo/json/', {json: JSON.stringify(jsonData), delay: 1}) .done(d => this.setState({comments: d['comments'], title: d['title']})); } render() { let comments = this.state.comments.map(c => (<li>{c.body} - <em>{c.author} </em> </li>)); return ( <div> <h2> {this.state.title} Comments </h2> <ul> {comments} </ul> </div> ); } } React.render(<CommentList />, document.getElementById('test'));
  4. The Solution! Use a container to handle the state and

    render the corresponding sub- component as a “pure” component.