React (Web Framework) Reference
Kip Landergren
(Updated: )
My cheat sheet for React covering patterns, idioms, and helpful resources.
Contents#
- Patterns
- Idioms
- Conventions
- Gotchas
- App File Structure / Directory Structure
- Custom Environment Variables
- Events
- JSX
- Resources
- FAQ
- Terminology
Patterns#
- lifting state up
- single source of truth
- controlled components
- top-down data flow
- single responsibility principle; used when identifying component boundaries
- DRY: don’t repeat yourself
- composition over inheritance
Idioms#
- use
on[Event]to represent props that are events - use
handle[Event]to represent methods that handle events
Conventions#
- prefer PascalCase.tsx for component PascalCase
Gotchas#
- do not mutate
stateinsetState() - deeply nested objects within
statemay not register updates
App File Structure / Directory Structure#
By feature “foo”:
./src/features/foo/fooSlice.js
./src/features/foo/Foo.js
./src/features/foo/Foo.module.css
By role:
./src/actions/
./src/components/
./src/containers/
./src/middleware/
./src/reducers/
./src/store
Custom Environment Variables#
Prefix with REACT_APP_ and access via process.env.REACT_APP_MY_VAR. At build time React will swap them in. More info in the documentation on adding custom environment variables
Events#
onChangeonFocusonInputonKeyUp
SyntheticEvent#
Mimics portions of the native Event interface.
Selected attribute explanations:
DOMEventTarget |
target |
the element on which the event occurred |
DOMEventTarget |
currentTarget |
the element the event handler has been attached to |
JSX#
Remember this example, straight from the docs, that JSX represents objects:
const element = (
<h1 className="greeting">
Hello, world!
</h1>
);
is equivalent to:
const element = React.createElement(
'h1',
{className: 'greeting'},
'Hello, world!'
);
Basic usage:
// creates a component referable as `Title`
const Title = (props) => {
const title = props.title;
// wrap in parens to reduce automatic semi-colon insertion errors
return (
<h2>
{/* jsx comment style (1 of 2) */}
{
// jsx comment style (2 of 2)
}
{title} {/* `title` can be untrusted; React DOM escapes before rendering.
remember that curly braces wrap to-be-evaluated JavaScript
expressions, and are _not_ for control flow (like .erb files).
*/}
</h2>
);
}
// and how to use:
const MySection = (props) => {
const title = props.title;
const content = props.content;
return (
{/* HTML attributes need to be converted (most just to camelCase) */}
<section
className="my-section"
>
{/* note: comments, like HTML, cannot go inside a tag */}
{/* non-HTML attributes are passed to the component as `props` */}
<Title
title={title}
/>
{content}
</section>
);
}
Resources#
- React Releases
- React Blog containing all important announcements
- React API Reference
- React Compiler
- Style Guides:
- Making setInterval Declarative with React Hooks
- Your Guide to React.useCallback()
- 10 lessons I've learned about handling React state over the last 7 years...
The History of Render Props, Higher Order Components, and Mixins#
- Mixins Are Dead. Long Live Composition
- Mixins Considered Harmful
- Higher-Order Components
- Cheng Lou - The State of Animation in React at react-europe 2015
- Use a Render Prop!
- Render Props
FAQ#
Why is the cursor jumping on my controlled component?#
Stop updating state asynchronously. See here and here.
How can I tell what hooks changed?#
- Open React Dev Tools
- Go to Settings
- Select tab Profiler
- Check box Record why each component rendered while profiling
The flamegraph will now display a message like: Hook 12 changed.
Where can I find what the hook numbers refer to? Why can’t I see hook numbers on the Components tab?#
- Go to Components tool
- Select the component in question
- Under Hooks, expand each and look in sections like: SelectorWithStoreAndSubscription
Terminology#
- component#(term component)
- 1.
A small, isolated, and reusable piece of code; typically maintaining state; typically represents a description of what you want to see on the screen.
Narrower:
Related:
- component class#(term component class)
- 1.
- component type#(term component type)
- 1.
- composition#(term composition)
- 1.
- controlled component#(term controlled component)
- 1.
- custom hook#(term custom hook)
- 1.
- description#(term description)
- 1.
- element#(term element)
- 1.
Lightweight description of what to render.
Related:
- functional component#(term functional component)
- 1.
Components without state; only with a render method.
Broader:
- higher order component#(term higher order component)
- 1.
A function that takes a component and returns another component.
Broader:
- hook#(term hook)
- 1.
- hydrate#(term hydrate)
- 1.
Rendering markup with data server-side before sending to client.
Related:
- Incremental Static Regeneration (ISR)#(term Incremental Static Regeneration)(term ISR)
- 1.
- JSX (JavaScript XML)#(term JSX)(term JavaScript XML)
- 1.
An extension of the JavaScript language syntax to include XML-like tags.
- key#(term key)
- 1.
A unique identifier that allows React to determine which items have changed / added / removed.
- mounting#(term mounting)
- 1.
When an component is rendered to the browser DOM for the first time.
Related:
- offscreen#(term offscreen)
- 1.
- props (properties)#(term props)(term properties)
- 1.
Are read-only; considered “model” data.
Related:
- pure component#(term pure component)
- 1.
- ref#(term ref)
- 1.
A mutable reference.
- render prop#(term render prop)
- 1.
A function prop that a component uses to know what to render; the function prop does not need to be actually named
render, render just refers to its purpose.Related:
- Server Side Rendering (SSR)#(term Server Side Rendering)(term SSR)
- 1.
- Static Site Generation (SSG)#(term Static Site Generation)(term SSG)
- 1.
- side effects#(term side effects)
- 1.
- state#(term state)
- 1.
Considered “model” data.
Related:
- suspense#(term suspense)
- 1.
- unmounting#(term unmounting)
- 1.
When an component is removed from the browser DOM.
Related: