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>

<h2>What’s going on here?</h2>
<p>Basically, the idea is to add a specific <a href="https://reactjs.org/docs/render-props.html" target="_blank" rel="noopener">render prop</a> for each piece in a component that you want to be able to swap out.</p>
<p>By default, my “SwappableComponent” uses the “DefaultButton” component for article buttons. But in cases where you want to completely replace the button, you can add a “buttonAs” prop—a render prop—that renders any other button component you like.</p>
<p>Bonus: Since <a href="https://reactjs.org/docs/components-and-props.html" target="_blank" rel="noopener">functional components</a> are just functions, you can simply pass the name of that component in “buttonAs” to swap them out.</p>

<h2>References</h2>
<ul>
  <li><a href="https://reactjs.org/docs/components-and-props.html" target="_blank" rel="noopener">React: Components and Props</a></li>
  <li><a href="https://reactjs.org/docs/composition-vs-inheritance.html" target="_blank" rel="noopener">React: Composition vs Inheritance</a></li>
  <li><a href="https://reactjs.org/docs/render-props.html" target="_blank" rel="noopener">React: Render Props</a></li>
  <li><a href="https://codepen.io/topic/react/templates" target="_blank" rel="noopener">CodePen: React Patterns & Templates</a></li>
</ul>

              
            
!

CSS

              
                body {
	margin-block-end: 10rem;
}

.article {
  border: 1px solid black;
  padding: 1rem;
}

.article > h3 {
  margin-block-start: 0;
}

              
            
!

JS

              
                const DefaultButton = ({ text }) => (
  <button type="button">{text}</button>
);

const AlternateButton = ({ text }) => (
  <button type="button" style={{ backgroundColor: 'blue', color: 'white' }}>
		🤯 {text} 🤯
	</button>
);

const SwappableComponent = ({ article, button: buttonProps, buttonAs: Button = DefaultButton, ...props }) => (
  <article className="article">
    <h3>{article.title}</h3>
    <p>{article.description}</p>
    {Button(buttonProps)}
  </article>
);

const App = () => (
  <main>
    <h1>“Swappable” React Components</h1>

    <h2>Default Component</h2>
    <SwappableComponent
      article={{
        title: 'Example with default button',
        description: 'This component only passes in basic article and button props.',
      }}
      button={{ text: 'Example Button' }}
    />

    <h2>Swapped Component</h2>
    <SwappableComponent
      article={{
        title: 'Example with swapped button',
        description: 'But this component passes an additional “buttonAs” prop that acts as a render function for the button component.',
      }}
      button={{ text: 'Example Button' }}
      buttonAs={({ text }) => (
        <button type="button" style={{ backgroundColor: 'red', color: 'white' }}>
          đź’Ą {text} đź’Ą
        </button>
      )}
    />
		
    <h2>Swapped Functional Component</h2>
    <SwappableComponent
      article={{
        title: 'Example with swapped button',
        description: 'But this component passes an additional “buttonAs” prop that acts as a render function for the button component.',
      }}
      button={{ text: 'Example Button' }}
      buttonAs={AlternateButton}
    />
  </main>
);

ReactDOM.render(<App />, document.querySelector('#root'));

              
            
!
999px

Console