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

              
                <div id="root"></div>
              
            
!

CSS

              
                #root {
    padding: 5px;

    .block-text {
        display: block;
        margin-top: 1rem;
        margin-bottom: 0.5rem;
    }

    .reusable-component {
        display: inline-block;
        padding: 5px;
        margin: 5px;
        border: 1px solid green;

        &--faulty {
            border-color: red;
        }
    }
}

              
            
!

JS

              
                /**
 * Creates new React Element on each re-render.
 * From https://github.com/facebook/react/blob/v16.13.1/packages/react-reconciler/src/ReactChildFiber.js#L844
 * to https://github.com/facebook/react/blob/v16.13.1/packages/react-reconciler/src/ReactChildFiber.js#L407
 *
 * Element type changes (reference equality, current.elementType === element.type):
 * -> Reconciliation sees it as a new component and replaces it and its children
 * -> Children lose their current state
 */
function FaultyReusableComponent(props) {
    const NestedComponent = () => (
        <div>
            <BlockText>NestedComponent</BlockText>
            {props.children}
        </div>
    );

    return (
        <div className="reusable-component reusable-component--faulty">
            <h2>Faulty Reusable Component</h2>
            <NestedComponent />
        </div>
    );
}

const OutsideDefinedSubComponent = (props) => (
    <div>
        <BlockText>OutsideDefinedSubComponent</BlockText>
        {props.children}
    </div>
);

function WorkingReusableComponent(props) {
    return (
        <div className="reusable-component">
            <h2>Working Reusable Component</h2>
            <OutsideDefinedSubComponent {...props} />
        </div>
    );
}

function App() {
    const [count, increase] = React.useReducer((s) => s + 1, 0);

    return (
        <div>
            <HelpText />

            <div>
                Count is {count} &nbsp;
                <button onClick={increase}>Increase</button>
                <HelperButton />
            </div>

            {/*
             * Notice how we are passing the same StatefulComponent to both
             * FaultyReusableComponent and WorkingReusableComponent.
             * However in FaultyReusableComponent the child keeps losing its state
             * when parent updates. WorkingReusableComponent works just fine!
             */}
            <FaultyReusableComponent>
                <StatefulComponent />
            </FaultyReusableComponent>

            <WorkingReusableComponent>
                <StatefulComponent />
            </WorkingReusableComponent>
        </div>
    );
}

function StatefulComponent() {
    const [value, onChange] = React.useState("Change me and click increase");

    return (
        <input
            type="text"
            value={value}
            onChange={(event) => onChange(event.target.value)}
        />
    );
}

/* Unrelated helpers below */

const HelpText = () => (
    <BlockText>
        Change texts and click increase
        <br />
        Expected: Texts do not reset
        <br />
        Actual: Faulty component resets its child's state
    </BlockText>
);
const BlockText = ({ children }) => <p className="block-text">{children}</p>;

const HelperButton = () => (
    <button
        onClick={() => {
            document
                .querySelectorAll("#root input[type=text]")
                .forEach((input) => {
                    Object.getOwnPropertyDescriptor(
                        window.HTMLInputElement.prototype,
                        "value"
                    ).set.call(input, "New text");
                    input.dispatchEvent(new Event("input", { bubbles: true }));
                });
        }}
    >
        Change all
    </button>
);

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console