Slide 1

Slide 1 text

The Tao of the React Component Props vs State

Slide 2

Slide 2 text

What is a React Component? • Basically, a function that transforms data into HTML. • The same data always renders the same HTML. * By data, we mean props and state. * By HTML, we mean a Virtual DOM node.

Slide 3

Slide 3 text

MyComponent = React.createClass({ contextTypes: { previousURL: React.PropTypes.string }, propTypes: { expanded: React.PropTypes.bool }, getDefaultProps: function() { return {expanded: false}; }, componentWillReceiveProps: function(props) { if(props.expanded) this.setState({url: this.context.previousURL}); }, shouldComponentUpdate: function(nextProps, nextState) { return nextProps.expanded !== this.props.expanded; }, render: function() { return ( A link ); } });

Slide 4

Slide 4 text

MyComponent = function(props, state) { return ( A link ); };

Slide 5

Slide 5 text

What is a React Component? • A React Component is also a state machine • Update a component’s state by calling setState() • Or by calling setProps() (on the top component only)

Slide 6

Slide 6 text

What are props? • Arguments to the Component render function • Props come from outside the Component

Slide 7

Slide 7 text

var Tab = React.createClass({ render: function() { return (
{this.props.children}
); } }); var active = false; (function renderTab() { React.render( Hello, World! , document.body ); active = !active; }());

Slide 8

Slide 8 text

What is State? • Also used in the Component render function • State is isolated inside the Component • State persists between renders

Slide 9

Slide 9 text

var Tab = React.createClass({ getInitialState: function() { return {active: false}; }, handleClick: function() { this.setState({active: !this.state.active}); }, render: function() { return (
{this.props.children}
); } }); (function renderTab() { React.render(Hello, World!, document.body); }());

Slide 10

Slide 10 text

So, when do you use props vs. state?

Slide 11

Slide 11 text

So, maybe you should primarily use state? Components are about encapsulation

Slide 12

Slide 12 text

So, maybe you should primarily use props! Components are about composition

Slide 13

Slide 13 text

So, when do you use props vs. state?

Slide 14

Slide 14 text

Unless you need to use state Always use props

Slide 15

Slide 15 text

Interactive Props • Some components are affected by user interactions • Since props are immutable, interactions that change the component must either be handled by the parent component, or translated to state

Slide 16

Slide 16 text

React Form Components React.createClass({ getInitialState: function() { return {value: 'Hello!'}; }, handleChange: function(event) { this.setState({value: event.target.value}); }, render: function() { return ( ); } });

Slide 17

Slide 17 text

Controlled Components • Values provided as props • Changes always come from above in the form of new props

Slide 18

Slide 18 text

var Tab = React.createClass({ render: function() { return (
{this.props.children}
); } }); var active = false; (function renderTab() { React.render( Hello, World! , document.body ); active = !active; }());

Slide 19

Slide 19 text

Uncontrolled Components • Values maintained in state • parent components might pass in callbacks to be notified of changes

Slide 20

Slide 20 text

var Tab = React.createClass({ getInitialState: function() { return {active: false}; }, handleClick: function() { this.setState({active: !this.state.active}); }, render: function() { return (
{this.props.children}
); } }); (function renderTab() { React.render(Hello, World!, document.body); }());

Slide 21

Slide 21 text

The best of both worlds: Controllables • An interactive component needs to maintain its own state • But sometimes it needs to yield control • Like React’s Form Components with interactive props • github.com/matthewwithanm/react-controllables

Slide 22

Slide 22 text

var Tab = React.createClass({ mixins: [ControllablesMixin], controllables: ['active'], getDefaultProps: function() {return {defaultActive: false};}, handleClick: function(event) { if (this.props.onClick) this.props.onClick(event); this.setState({active: !this.state.active}); }, render: function() { return (
{this.props.children}
); } }); (function renderTabs(active) { React.render(
Hello, World!, Goodbye, World!,
, document.body); }());

Slide 23

Slide 23 text

Finally, a word about PropTypes • Props are your Component API • Validate usage of your Component • Document usage of your Component

Slide 24

Slide 24 text

var Tab = React.createClass({ displayName: 'Tab', propTypes: { active: React.PropTypes.bool, onClick: React.PropTypes.func }, render: function() { return (
{this.props.children}
); } });

Slide 25

Slide 25 text

More words about PropTypes • React provides the most common PropTypes under React.PropTypes • Custom PropTypes are possible, but sometimes it’s a bit tricky to fully emulate what the builtin types do • github.com/gcanti/tcomb-validation • flowtype.org

Slide 26

Slide 26 text

HZDG.COM LETTERTWO ERIC ELDREDGE [email protected] HZDG PHOTO MISSING