React Playground: Write and Run React Code Online
What is a React playground?
It’s a place to write a React component and see it work straight away, without creating a project, installing
Node.js or running npm. Type JSX on the left and the result shows up on the right. This is a complete app here:
export default function App() {
return <h1>Hello, Sam</h1>;
}
It shows a big “Hello, Sam” heading in the Preview. Change “Sam” to your own name and the Preview changes a moment
after you stop typing. Buttons, inputs and state all work, because it’s real React (version 19.2.9) running
in a real page.
How to Use It
Step 1 Start from a sample or a blank file
The first time you open the page, the Coffees today counter is already running. Open
Samples for a to-do list, a form input that says hello, a tea timer with
useEffect and a coffee menu built with .map(). Or clear the editor and write your own.
Step 2 Watch the Preview
Auto run is on, so the app restarts a moment after you stop typing. Untick it if you’d rather
press the lime Run button (or Ctrl + Enter, Cmd +
Enter on a Mac). Each run starts fresh, so state goes back to its starting value.
Step 3 Check the Console
Anything your components print with console.log() goes to the Console tab, along
with React’s own warnings. A dot on the tab means there’s something new to read. Errors also appear in a red box
over the Preview, with a button that jumps to the line.
Step 4 Style it in the CSS tab
The CSS tab is a normal stylesheet for the page. Use className in your JSX (not
class) and match it in the CSS:
<p className="note">Maybe switch to water?</p>
.note {
color: #b45309;
}
Step 5 Share it with Copy link
Copy link makes a link with your code packed inside it (after the #). Whoever
opens it sees the same code, with a note to read it and then press Run: code from a link never runs by itself,
and it doesn’t replace their own saved code (a Back to your own code button brings that back).
Your code is also saved in this browser, so it’s still there next time.
What runs where
Everything happens in your browser. Your JSX is turned into plain JavaScript on this page, React is loaded from this
website, and your app runs inside a locked frame. That frame can’t read this website, its cookies or anything you’re
signed in to, and nothing you type is uploaded. It’s the same kind of box the
JavaScript Playground uses, so it’s safe to paste code from a tutorial to see
what it does.
What your code needs
A component to show
The playground shows your default export. If there isn’t one, it looks for a function called
App. That means both export default function App() and a plain
function App() work.
Imports from react and react-dom
Import hooks and anything else from React the same way you would in a project:
import { useState, useEffect } from 'react';
export default function App() {
const [cups, setCups] = useState(1);
useEffect(() => {
document.title = cups + ' cups';
}, [cups]);
return <button onClick={() => setCups(cups + 1)}>Cups: {cups}</button>;
}
react, react-dom and react-dom/client are available. Other packages aren’t,
because there’s no npm here. window.React is set too, for older examples that write
React.useState without an import.
Or make the root yourself
Some tutorials end with their own createRoot() call. That works as it is: the page has a
<div id="root">, and the playground won’t render anything else on top.
import { createRoot } from 'react-dom/client';
function Greeting() {
return <p>Hello from my own root</p>;
}
createRoot(document.getElementById('root')).render(<Greeting />);
TypeScript with TSX
Tick TSX and the file becomes App.tsx, so you can type your props:
type Props = { name: string; cups?: number };
function Order({ name, cups = 1 }: Props) {
return <p>{name} ordered {cups} cups</p>;
}
export default function App() {
return <Order name="Sam" cups={2} />;
}
The types are removed before the code runs, not checked. To see exactly what gets removed, paste the same code into
the TypeScript Playground with TSX ticked.
React’s warnings are on
The playground uses React’s development build, the one that explains mistakes. Run this and the Console shows a
yellow warning about a missing key, with a tip:
const drinks = ['Latte', 'Mocha', 'Tea'];
export default function App() {
return (
<ul>
{drinks.map((d) => <li>{d}</li>)}
</ul>
);
}
The fix is one attribute: {drinks.map((d) => <li key={d}>{d}</li>)}. React uses the key to tell list items apart when the list
changes, so it should be something that belongs to the item (an id, or a name that never repeats) rather than its
position.
Errors and warnings you’ll probably meet
An error stops your app. It shows in a red box over the Preview and in red in the Console, each time
with a short tip, and the red number on the Console tab counts it.
| The message says | It usually means |
Objects are not valid as a React child | You put a whole object in { } inside JSX. Show one field, like {user.name}. |
Too many re-renders | Usually onClick={setCount(1)}. Write onClick={() => setCount(1)} so it only runs on a click. |
Rendered more hooks than during the previous render | A hook (useState, useEffect…) is inside an if or after an early return. Hooks go at the top of the component. |
Element type is invalid | A component you used is undefined: a typo, or a default vs { named } import mix-up. |
Maximum update depth exceeded | A useEffect sets state on every run, so it never stops. The playground stops your app for you. Add a dependency list. |
Only react and react-dom can be imported here | This playground has no npm install. Paste the code you need into the file instead. |
A warning doesn’t stop anything, but it points at a real mistake. Warnings show in yellow in the
Console with a tip, and they don’t open the red box.
| The message says | It usually means |
Each child in a list should have a unique "key" prop | Give each item from .map() a key that belongs to it, like key={drink.id}. |
A component is changing an uncontrolled input to be controlled | The input’s value started as undefined. Start the state with an empty string: useState(''). |
You provided a `value` prop to a form field without an `onChange` handler | Add an onChange that updates the state, or the input can’t be typed in. |
How this differs from a real React project
- One file plus CSS. A project splits components across many files and imports them. Here
everything lives in one file, so put all your components in it.
- No npm install. Only React and ReactDOM are there. Routers, state libraries and UI kits need a
real project.
- No build step. In a project, a build tool bundles your files, checks types if you use TypeScript
and makes a small production build. Here your code is turned into JavaScript on the fly and run straight away.
- No server. Server components, API routes and anything that needs a backend can’t run here.
fetch() to a public API that allows it does work.
- Storage is switched off.
localStorage and cookies throw an error inside the locked
frame, so keep state in useState.
Learn more
Frequently Asked Questions
Do I need to install anything to try React here?
No. There’s no Node.js, no npm and no project to set up. Write a component in the editor and it shows up in the Preview. React itself is loaded from this website, and your JSX is turned into JavaScript in your browser.
Is my code uploaded or saved anywhere?
Your code stays in your browser. It runs there, inside a locked frame that can’t reach this website, its cookies or your storage, and it’s kept in this browser so it’s still there when you come back. Nothing is sent to our server. Copy link puts the code inside the link itself, so whoever opens the link gets the code from the link, not from us.
Which React version does the playground use?
React 19.2.9 and ReactDOM 19.2.9, the development build. That’s the build that prints React’s helpful warnings, like a missing key in a list, and full error messages instead of short error codes.
Can I import other npm packages?
Only react and react-dom (including react-dom/client) can be imported. Anything else gives a short error, because there’s no npm install here. An import of a .css file is skipped with a note: put the CSS in the CSS tab instead.
Why does my component not show up?
The playground shows the default export, or a function called App if there’s no default export. If your code has neither, the Preview says so. If you call createRoot() yourself, that works too, and the playground doesn’t render anything on top of it. An error shows up in a red box over the Preview and in the Console tab.
Can I use TypeScript (TSX)?
Yes. Tick TSX and the file becomes App.tsx, so you can add types to props and state. Types are removed before the code runs, not checked, so a wrong type won’t stop it. To see what TypeScript turns into, try the TypeScript Playground.
How do I share my React code?
Click Copy link. The link holds your code (and your CSS), so opening it shows the same code. It doesn’t run until the person reads it and presses Run, and it never replaces their own saved code. Very long code makes a very long link, and some chat apps cut those, so the playground warns you when a link gets over about 8 KB.
Where do console.log() messages from my components go?
Into the Console tab, along with React’s warnings and any errors. A small dot on the Console tab means new messages arrived while you were looking at the Preview, and a red number counts the errors.