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

              
                #app
              
            
!

CSS

              
                * {
  box-sizing: border-box
}

pre, code {
  font-family: monospace
  padding: .5em
  background: #eee
  border-radius: .2em
}

code {
  color: #e76ea1 - 20
  padding: .25em .5em
}

p {
  margin: 0 0 .5em
}

#app {
  padding: 1em
}

.flex {
  display: flex
  //flex-wrap: wrap
}

.flex-item {
  flex-grow: 1
  padding: 0 1em 1em
}
              
            
!

JS

              
                const useState    = React.useState;
const useMemo     = React.useMemo;
const useCallback = React.useCallback;

function Counter({ count, square, onClick, onReset }) {
  return (
    <div>
      <p>Count: {count}</p>
      <p>Square: {square}</p>
      <button onClick={onClick}>+1</button> 
      <button onClick={onReset}>reset</button>
    </div>
  );
}

// 🐞
// ----------------------------------------------------
function CounterWithBug({ initCount }) {
  const [count, setCount] = useState(initCount);
  const [square, setSquare] = useState(initCount * initCount);
  
  const updateSquare = () => {
    setSquare( count * count );
  }
  
  const onCountup = () => {
    setCount( count + 1 );
    updateSquare();
  }
  
  const onReset = () => {
    setCount( initCount );
    setSquare( initCount * initCount );
  }
  
  return (
    <>
      <span role="img">🐞</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const onCountup = () => {
  setCount( count + 1 );
  setSquare( count * count );
}`
        }</pre>
    </>
  );
}

// 👍
// ----------------------------------------------------
function CounterWithVar({ initCount }) {
  const [count, setCount] = useState(initCount);
  const [square, setSquare] = useState(initCount * initCount);
  
  const updateSquare = ( val ) => {
    setSquare( val * val );
  }
  
  const onCountup = () => {
    const newCount = count + 1;
    setCount( newCount );
    updateSquare( newCount );
  }
  
  const onReset = () => {
    setCount( initCount );
    setSquare( initCount * initCount );
  }
  
  return (
    <>
      <span role="img">👍</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const onCountup = () => {
  const newCount = count + 1
  setCount( newCount );
  setSquare( newCount * newCount );
}`
        }</pre>
    </>
  );
}

// 👍 use Setter function
// ----------------------------------------------------
function CounterWithSetter({ initCount }) {
  const [count, setCount] = useState(initCount);
  const [square, setSquare] = useState(initCount * initCount);
  
  const updateSquare = ( val ) => {
    setSquare( val * val );
  }
  
  const onCountup = () => {
    setCount( (preCount) => {
      const newCount = preCount + 1;
      updateSquare( newCount );
      return newCount;
    } );
    
  }
  
  const onReset = () => {
    setCount( initCount );
    setSquare( initCount * initCount );
  }
  
  return (
    <>
      <span role="img">👍</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const onCountup = () => {
  setCount(preCount => {
    const newCount = preCount + 1;
    setSquare( newCount * newCount );
    return newCount;
  });
}`
        }</pre>
    </>
  );
}

// ⚠️ use Callback Anti pattern
// ----------------------------------------------------
function CounterWithUseCallbackAntiPattern({ initCount }) {
  const [count, setCount] = useState(initCount);
  const square = useMemo(() => {
    return count * count;
  }, [count]);
  
  // generate function everytime when count changes.
  const onCountup = useCallback(() => {
    setCount( count + 1 );
  }, [count]);
  
  const onReset = useCallback(() => {
    setCount( initCount );
  }, [initCount]);
  
  return (
    <>
      <span role="img">⚠️ useCallback</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const onCountup = useCallback(() => {
    setCount( count + 1 );
  }, [count]);`
        }</pre>
      <p>generate function everytime when <code>count</code> changes.</p>
    </>
  );
}

// 🐞use Callback
// ----------------------------------------------------
function CounterWithUseCallbackHasBug({ initCount }) {
  const [count, setCount] = useState(initCount);
  const square = useMemo(() => {
    return count * count;
  }, [count]);
  
  const onCountup = useCallback(() => {
    // 🐞count bind first time value.
    setCount( count + 1 );
  }, []);
  
  const onReset = useCallback(() => {
    setCount( initCount );
  }, [initCount]);
  
  return (
    <>
      <span role="img">🐞useCallback</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const onCountup = useCallback(() => {
    setCount( count + 1 );
  }, []);`
        }</pre>
      <p>function create once, but <code>count</code> var was binded first value.</p>
    </>
  );
}

// 👍use Callback
// ----------------------------------------------------
function CounterWithUseCallback({ initCount }) {
  const [count, setCount] = useState(initCount);
  const square = useMemo(() => {
    return count * count;
  }, [count]);
  
  const onCountup = useCallback(() => {
    setCount( preCount => preCount + 1 );
  }, []);
  
  const onReset = useCallback(() => {
    setCount( initCount );
  }, [initCount]);
  
  return (
    <>
      <span role="img">👍useCallback</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const onCountup = useCallback(() => {
    setCount( preCount => preCount + 1 );
  }, []);`
        }</pre>
    </>
  );
}

// 👍 use Memo
// ----------------------------------------------------
function CounterWithUseMemo({ initCount }) {
  const [count, setCount] = useState(initCount);
  const square = useMemo(() => {
    return count * count;
  }, [count]);
  
  const onCountup = () => {
    setCount( preCount => preCount + 1 );
  }
  
  const onReset = () => {
    setCount( initCount );
  }
  
  return (
    <>
      <span role="img">👍 useMemo</span>
      <Counter
        count={count}
        square={square}
        onClick={onCountup}
        onReset={onReset}
       />
      <pre>{
 `const square = useMemo(() => {
    return count * count;
  }, [count]);

const onCountup = () => {
  setCount( preCount => preCount + 1 );
}
`
        }</pre>
    </>
  );
}


// ----------------------------------------------------------------------
const initCount = 2;
function App() {
  return (
    <>
      <div className="flex">
        <div className="flex-item">
          <CounterWithBug initCount={initCount} />
        </div>
        <div className="flex-item">
          <CounterWithVar initCount={initCount} />
        </div>
        <div className="flex-item">
          <CounterWithSetter initCount={initCount} />
        </div>
      </div>
      <p>useMemo / useCallback</p>
      <div className="flex">
        <div className="flex-item">
          <CounterWithUseMemo initCount={initCount} />
        </div>
        <div className="flex-item">
          <CounterWithUseCallbackAntiPattern initCount={initCount} />
        </div>
        <div className="flex-item">
          <CounterWithUseCallbackHasBug initCount={initCount} />
        </div>
        <div className="flex-item">
          <CounterWithUseCallback initCount={initCount} />
        </div>
      </div>
    </>
  );
}

ReactDOM.render(<App />, document.getElementById('app'));
              
            
!
999px

Console