code-playpad

React.js Labs

A multi-file React project in a sandboxed preview, with its console underneath. Change a file and press Run, or Ctrl/⌘+Enter. Drag the dividers to resize the sections, or press the window button in its toolbar to give it a whole window.

Jump to: React basics · Hooks

Files as tabs

The default.

body { font: 16px system-ui, sans-serif; margin: 16px; } li { margin: 4px 0; }

Files as a tree

Add file-view="tree". Click a folder to open or close it. The output gets its own row under the tree and the code, and you can drag the dividers between them.

body { font: 16px system-ui, sans-serif; margin: 16px; } li { margin: 4px 0; }

Choose the tree's width

The tree starts at 20% of the width. Set tree-width to change that, with a percentage such as tree-width="35%" or a number of pixels such as tree-width="240". In the React wrapper, it's treeWidth. Readers can still drag the divider, and double-clicking it goes back to your value. This example starts at 35%:

Let readers switch the view

Add file-view-switch to show a toolbar button that switches between tabs and the tree. In the React wrapper, it's fileViewSwitch. file-view still sets the view it starts in.

Let readers change the files

Add file-actions and readers can add, rename and remove files from inside the widget. It's off by default. In the React wrapper, it's fileActions. Reset brings back the original files.

In a tree

Use the New file and New folder buttons above the files, or the + on a folder. To choose where they add, click a folder, or click Files for the top level. Each file you can change has a ⋯ button to rename or remove it.

In tabs

The + ends the row. Tabs have no folder rows, so type the folder in the name, such as src/components/TaskLine.jsx.

React basics

Components and props · Lists and conditional rendering · Forms · Lifting state up

Components and props

Split a page into components, in separate files, and pass them data.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

Lists and conditional rendering

Filter a list as you type, and show a message when nothing matches.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

Forms

A controlled form: state holds each field, and submitting checks it.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

Lifting state up

Two inputs stay in step because their shared parent owns the state.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

Hooks

useState · useEffect · useRef · useReducer · useContext · useMemo and useCallback · useId · Your own hook

useState

Remembers a value between renders. Pass a function when the next value depends on the last one.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

useEffect

Keeps something outside React in step, and cleans up after itself. Watch the console as you press Pause.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

useRef

Holds a value that survives renders without causing one, or a reference to a DOM element.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

useReducer

Puts every state change in one function, which suits state with several kinds of update.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

useContext

Hands a value to any component below, without passing props through every level.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

useMemo and useCallback

Keep a slow result, or a function, between renders. Clicking +1 logs nothing new, because neither the list nor the child needs redoing.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

useId

Makes a unique id, so a component used twice still links each label to its own input.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }

Your own hook

A function starting with "use" can package hooks for reuse. Each component calling it gets its own state.

body { font: 16px/1.5 system-ui, sans-serif; margin: 16px; color: #1f2328; } button { font: inherit; padding: 4px 12px; margin-right: 6px; } input, select { font: inherit; padding: 4px 6px; } .card { border: 1px solid #d0d7de; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .muted { color: #656d76; } .error { color: #b42318; } .done { text-decoration: line-through; color: #656d76; }