Skip to content

Instantly share code, notes, and snippets.

@seanrankin
Last active November 9, 2018 03:28
Show Gist options
  • Save seanrankin/0c8f54a0ca4d5d9431d3cc9f59d7b3f3 to your computer and use it in GitHub Desktop.
Save seanrankin/0c8f54a0ca4d5d9431d3cc9f59d7b3f3 to your computer and use it in GitHub Desktop.
// React Notes
///////////////////////////////////////////////////////////////////////////
// Render Props
// The term “render prop” refers to a simple
// technique for sharing code between React components
// using a prop whose value is a function.
///////////////////////////////////////////////////////////////////////////
<DataProvider render={data => ( <h1>Hello {data.target}</h1> )} />
///////////////////////////////////////////////////////////////////////////
// Refs API
///////////////////////////////////////////////////////////////////////////
// Setting a ref that can be called with this.myNode:
<div ref={node => (this.myNode = node)} />
// Sometimes you need to assign them wihin iterators:
<Component ref={node => { this[`node-${index}`] = node }} />
// You can even use the callback to do fun things like get getBoundingClientRect():
<div ref={this.refCallback} />
///////////////////////////////////////////////////////////////////////////
// State
///////////////////////////////////////////////////////////////////////////
// Use the setState callback to access previous state (avoids batching issue)
this.setState(prevState => {
return { count: prevState.count++ };
});
///////////////////////////////////////////////////////////////////////////
// New Context API
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Hooks
///////////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment