Pen Settings



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


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


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.


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.


                <h1>useFetch custom React hook</h1>
  This demonstrates implementing a custom React hook that makes it
  super simple to deal with fetch requests.

  Please note, React’s new Suspense feature will be better for handling
  fetching data, this is just for learning purposes. Thanks
  <a href="">for the tip</a> Dan.

<div id="app"></div>


                body {
  margin: 16px;

button {
  border: none;
  background: blueviolet;
  color: white;
  display: inline-block;
  padding: 0.5rem 1rem;
  border-radius: 1rem;

ul {
  list-style: none;
  padding-left: 0;
  li {
    margin-bottom: 8px;  
  input[type="checkbox"] {
    margin-right: 8px;


                const { useReducer, useEffect, useState } = React;

const App = () => {
  const url = "";
  const { loading, error, data, refetch } = useFetch(url);
  if (loading) {
    return <p>Loading...</p>;
  if (error) {
    return <p>Error: {error.message}</p>;
  return (
      <button onClick={refetch}>Refetch</button>
        { => (
              <input type="checkbox" defaultChecked={todo.completed} />

const useFetch = (url) => {
  const types = {
  const [state, dispatch] = useReducer((state, action) => {
    switch(action.type) {
      case types.REQUEST:
        return {
          loading: true,

      case types.SUCCESS:
        return {
          loading: false,

      case types.ERROR:
        return {
          loading: false,
          error: action.error,
      case types.REFETCH:
        return {
          refetch: state.refetch + 1,

        return state;
  }, {
    loading: true,
    error: undefined,
    data: undefined,
    refetch: 0,
  useEffect(() => {
    dispatch({ type: types.REQUEST });
      .then(response => {
        if (!response.ok) {
          throw new Error(`Oops! There was a ${response.status} error`);
        return response.json();
      .then(data => dispatch({ type: types.SUCCESS, data }))
      .catch(error => dispatch({ type: types.ERROR, error }))
  }, [state.refetch]);
  return {
    refetch: () => dispatch({ type: types.REFETCH }),