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

BristolJS: An Introduction to React

BristolJS: An Introduction to React

Jack Franklin

April 27, 2016
Tweet

More Decks by Jack Franklin

Other Decks in Technology

Transcript

  1. // Input (JSX): var app = <Nav color="blue" />; //

    Output (JS): var app = React.createElement( Nav, { color: "blue" } );
  2. var HelloWorld = React.createClass({ render: function() { return <h1>Hello World!</h1>;

    } }) ReactDOM.render( <HelloWorld />, document.getElementById('app') )
  3. var HelloWorld = React.createClass({ render: function() { return <h1>Hello World!</h1>;

    } }) ReactDOM.render( <HelloWorld />, document.getElementById('app') )
  4. var HelloWorld = React.createClass({ render: function() { return <h1>Hello World!</h1>;

    } }) ReactDOM.render( <HelloWorld />, document.getElementById('app') )
  5. var Person = React.createClass({ render: function() { return ( <div>

    <h2>Name: { this.props.name }</h2> <p>Fav colour: { this.props.colour }</p> </div> ); } });
  6. var OnOff = React.createClass({ getInitialState: function() { return { on:

    false } }, render: function() { return ( <div> <button>Toggle</button> <p>{ this.state.on ? 'ON' : 'OFF'}</p> </div> ) } })
  7. var OnOff = React.createClass({ getInitialState: function() { return { on:

    false } }, render: function() { return ( <div> <button>Toggle</button> <p>{ this.state.on ? 'ON' : 'OFF'}</p> </div> ) } })
  8. var OnOff = React.createClass({ ... render: function() { return (

    <div> <button onClick={this.toggle}> Toggle </button> </div> ) } })
  9. var ShowName = function(props) { return <p>Your name is {

    props.name } </p>; } ReactDOM.render( <ShowName name="jack" />, document.body )
  10. var ShowName = function(props) { return <p>Your name is {

    props.name } </p>; } ReactDOM.render( <ShowName name="jack" />, document.body ) http://jsbin.com/fawafahaqo/1/edit?js,output
  11. “This pattern is designed to encourage the creation of these

    simple components that should comprise large portions of your apps.”
  12. “In the future, we’ll also be able to make performance

    optimizations … by avoiding unnecessary checks and memory allocations” https://facebook.github.io/react/blog/2015/09/10/react-v0.14- rc1.html
  13. omponents in components in components in components in co omponents

    in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co omponents in components in components in components in co components in components in components in components in c
  14. var Mailbox = React.createClass({ getInitialState: function() { return { messages:

    [{ id: 1, title: 'Hello', content: 'How is it going?', sender: 'Alice' }, {…}] } },
  15. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  16. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  17. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  18. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  19. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  20. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  21. var messages = this.state.messages; var mails = messages.map(function(message) { return

    ( <li key={message.id}> <Message message={message} /> </li> ); }) return <ul>{mails}</ul>;
  22. var Message = React.createClass({ render() { return ( <h4> {

    this.props.message.title } </h4> ); } });
  23. var Person = React.createClass({ render() { return <p>{this.props.name} has a

    favourite colour of {this.props.colour} </p> } }) ReactDOM.render( <Person name="jack" colour="blue" />, document.body )
  24. var types = React.PropTypes; var Person = React.createClass({ propTypes: {

    name: types.string.isRequired, colour: types.string.isRequired },
  25. var types = React.PropTypes; var Person = React.createClass({ propTypes: {

    name: types.string.isRequired, colour: types.string.isRequired },
  26. var types = React.PropTypes; var Person = React.createClass({ propTypes: {

    name: types.string.isRequired, colour: types.string.isRequired },
  27. var GitHubPerson = React.createClass({ getInitialState: function() { return { data:

    {} }; }, componentWillMount: function() { getData().then(function(data) { this.setState({ data: data }) }.bind(this)); },
  28. var GitHubPerson = React.createClass({ getInitialState: function() { return { data:

    {} }; }, componentWillMount: function() { getData().then(function(data) { this.setState({ data: data }) }.bind(this)); },
  29. var GitHubPerson = React.createClass({ getInitialState: function() { return { data:

    {} }; }, componentWillMount: function() { getData().then(function(data) { this.setState({ data: data }) }.bind(this)); },
  30. var GitHubPerson = React.createClass({ getInitialState: function() { return { data:

    {} }; }, componentWillMount: function() { getData().then(function(data) { this.setState({ data: data }) }.bind(this)); },
  31. As the requirements for JavaScript single-page applications have become increasingly

    complicated, our code must manage more state than ever before
  32. One of the best (and overlooked) things about React is

    how much code you write that's just plain old JavaScript.