Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section id="usestate"></section>
<section id="useeffect"></section>
<section id="useref"></section>
              
            
!

CSS

              
                section {
  background-color: lightblue;
  border: 1px solid darkblue;
  padding: 5px;
  margin: 5px;
}
              
            
!

JS

              
                import * as ReactDOM from "https://cdn.skypack.dev/react-dom@18.2.0";
import * as React from "https://cdn.skypack.dev/react@18.2.0";

// useState
function App() {
  const [color, setColor] = React.useState("yellow");
  
  const [car, setCar] = React.useState({
    brand: "Ford",
    model: "Mustang",
    year: "1964",
    color: "red"
  });
  
    const updateColor = () => {
    setCar(previousState => {
      return { ...previousState, color: "green" }
    });
  }
  
  return <>
    <h1>String</h1>
    <p>My favorite color is {color}!</p>
    <button
        type="button"
        onClick={() => setColor("blue")}
      >Blue</button>
    <br/>
    <h1>Object</h1>
    <p>My {car.brand} is a {car.color} {car.model} from {car.year}.
      </p>
      <button
        type="button"
        onClick={updateColor}
      >Green</button>
  </>
}

// useEffect
function Counter() {
  const [count, setCount] = React.useState(0);
  const [calculation, setCalculation] = React.useState(0);

  React.useEffect(() => {
    setCalculation(() => count * 2);
  }, [count]); // if count updates, update calculation variable

  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount((c) => c + 1)}>+</button>
      <p>Calculation: {calculation}</p>
    </>
  );
}

function Timer() {
  const [count, setCount] = React.useState(0);

  React.useEffect(() => {
    let timer = setTimeout(() => {
      setCount((count) => count + 1);
  }, 1000);

  return () => clearTimeout(timer)
  }, []);

  return <h1>I've rendered {count} times!</h1>;
}

// useRef
function Pointer() {
  const inputElement = React.useRef();

  const focusInput = () => {
    inputElement.current.focus();
  };

  return (
    <>
      <p>
        <input type="text" ref={inputElement} />
        <button onClick={focusInput}>Focus Input</button>
      </p>
    </>
  );
}

function Before() {
  const [inputValue, setInputValue] = React.useState("");
  const previousInputValue = React.useRef("");

  React.useEffect(() => {
    previousInputValue.current = inputValue;
  }, [inputValue]);
  
  return (
    <>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
      />
      <h2>Current Value: {inputValue}</h2>
      <h2>Previous Value: {previousInputValue.current}</h2>
    </>
  );
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,  document.getElementById("usestate")
)

ReactDOM.render(
  <React.StrictMode>
    <Counter />
    <Timer />
  </React.StrictMode>,  document.getElementById("useeffect")
)

ReactDOM.render(
  <React.StrictMode>
    <Pointer />
    <Before />
  </React.StrictMode>,  document.getElementById("useref")
)
              
            
!
999px

Console