Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <script>
/**
 * Shallow Equal implementation
 * ============================
 * by github.com/NoamELB/shouldComponentUpdate-Children
 * install by:
 * npm i -S shouldcomponentupdate-children
 */
function useShallowEqual(WrappedComponent) {
    class ShallowEqualEnhancer extends WrappedComponent {
        shouldComponentUpdate(nextProps, nextState) {
            let shouldUpdate = false;
            if (!super.shouldComponentUpdate || super.shouldComponentUpdate(nextProps, nextState)) {
                if (!shallowEqualState(this.state, nextState) || !shallowEqualWithoutReactElements(this.props, nextProps)) {
                    shouldUpdate = true;
                }
            }
            return shouldUpdate;
        }
    }
    ShallowEqualEnhancer.displayName = `ShallowEqualEnhanced${WrappedComponent.displayName || WrappedComponent.name || 'Component'}`;

    return ShallowEqualEnhancer;
}

/**
 * @param {Object} thisState
 * @param {Object} nextState
 * @returns {Boolean}
 */
function shallowEqualState(thisState, nextState) {
    return thisState === nextState
}

/**
 * Perform a shallow equal to every prop that is not a React Element
 * This will return true for unchanged props (where the only changes are the react elements props like 'children')
 * @param {Object} thisProps
 * @param {Object} nextProps
 * @returns {Boolean}
 */
function shallowEqualWithoutReactElements(thisProps, nextProps) {
    let equals = false;
    if (thisProps === nextProps) {
        equals = true;
    } else if (typeof thisProps === 'object' && typeof nextProps === 'object') {
        equals = true;
        const propNames = new Set(Object.keys(thisProps), Object.keys(nextProps));
        for (const propName of propNames) {
            if (thisProps[propName] !== nextProps[propName] && !isReactElement(thisProps[propName])) {
                // No need to check nextProps[propName] as well, as we know they are not equal
                equals = false;
                break;
            }
        }
    }
    return equals;
}

/**
 * If the provided argument is a valid react element or an array that contains at least
 * one valid react element in it
 * @param {*} suspectedElement
 * @returns {Boolean}
 */
function isReactElement(suspectedElement) {
    let isElem = false;
    if (React.isValidElement(suspectedElement)) {
        isElem = true;
    } else if (Array.isArray(suspectedElement)) {
        for (let i = 0, l = suspectedElement.length; i < l; i++) {
            if (React.isValidElement(suspectedElement[i])) {
                isElem = true;
                break;
            }
        }
    }
    return isElem;
}
</script>
              
            
!

CSS

              
                
              
            
!

JS

              
                let [c1,c2,c3,c4,c5] = [0,0,0,0,0];
/**
 * Examples for this github: 
 * github.com/NoamELB/shouldComponentUpdate-Children
 */

/**
 * One of this component's props actually changes
 * in App and cause a render
 */
class ComponentThatActuallyChanges extends React.Component {
  render() {
    return `Component That Actually Changes, times rendered: ${++c1}`;
  }
}

/**
 * This component is rendered too even though
 * shouldComponentUpdate is implemented
 */
class ComponentThatShouldNotRender extends React.Component {
  shouldComponentUpdate(nextProps, nextState) {
    // like writing return true
    return this.props.children !== nextProps.children;    
  }
  render() {
    return `Component That should not render, times rendered: ${++c2}`;
  }
}

/**
 * PureComponent, nothing is changing and it still renders?
 */
class PureComponent1 extends React.PureComponent {
  render() {
    return `Pure Component with children, times rendered: ${++c3}`;
  }
}

/**
 * Variation with a prop
 */
class PureComponent2 extends React.PureComponent {
  render() {
    return `Pure Component with a React Element prop, times rendered: ${++c4}`;
  }
}

/**
 * Variation with a prop
 */
class IamNotRenderingThatEasily extends React.Component {
  render() {
    return `I'm using a custom shouldComponentUpdate implementation, times rendered: ${++c5}`;
  }
}

// useShallowEqual install by: "npm i -S shouldcomponentupdate-children"
// import useShallowEqual from "shouldcomponentupdate-children"
const WrappedIamNotRenderingThatEasily = useShallowEqual(IamNotRenderingThatEasily);

/**
 * This mimic a "container" component, it is renders a lot
 * and most of the time for a change just in one component..
 */
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    const callMe = () => {
      this.setState({time: Date.now()});
      setTimeout(callMe, 1000);
    };
    callMe();
  }
  render() {
    return (
      <div>
        <ComponentThatActuallyChanges changedProp={this.state.time} />
        <br />
        <ComponentThatShouldNotRender>
          <div />
        </ComponentThatShouldNotRender>
        <br />
        <PureComponent1>
          <div />
        </PureComponent1>
        <br />
        <PureComponent2 randomProp={<div />}/>
        <br />
        <WrappedIamNotRenderingThatEasily>
          <div />
        </WrappedIamNotRenderingThatEasily>
      </div>
    );
  }
}

const root = document.createElement('div');
document.body.appendChild(root);
ReactDOM.render(<App />, root);
              
            
!
999px

Console