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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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





              
            
!

CSS

              
                #Parent {
  background-color: lightgray;
  border: 1px solid black;
}

#Child1 {
  background-color: lightblue;
  border: 1px solid blue;
}

#Child2 {
  background-color: pink;
  border: 1px solid red;
}

#Child3 {
  background-color: lightgreen;
  border: 1px solid green;
}

.box {
  padding: 5px;
  margin: 5px;
  border-radius: 5px;
}

.child-box {
  width: 500px;
  margin: 20px;
  box-shadow: 3px 3px 3px 3px #b8b8b8;
}
              
            
!

JS

              
                const Child1 = ({ countFromParent }) => {
  return (
    <div id='Child1' className='box child-box'>
      <h2>This is child 1.</h2>
      <p>It knows the count from the parent.</p>
      <p>Count from Parent = { countFromParent }</p>
    </div>
  )
}
      
const Child2 = ({ incrementParent, decrementParent }) => {
  return (
    <div id='Child2' className='box child-box'>
      <h2>This is child 2.</h2>
      <p>It can affect the state of the parent because it received the callback functions from the parent.</p>
      <button onClick={ incrementParent }>Increment Parent</button>
      <button onClick={ decrementParent }>Decrement Parent</button>
    </div>
  )
}
      
const Child3 = ({ countFromParent, incrementParent, decrementParent }) => {
  // Using the useState hook, as opposed to a class...
  const [selfCount, setSelfCount] = React.useState(42)
  const incrementSelf = () => setSelfCount(c => c + 1)
  const decrementSelf = () => setSelfCount(c => c - 1)
  return (
    <div id='Child3' className='box child-box'>
      <h2>This is child 3.</h2>
      <p>It can receive and change the state of the parent but also has its own state.</p>
      <p>For fun, it uses a functional component with the <i>useState</i> hook instead of a class.</p>
      <p>Own Count = {selfCount}</p>
      <button onClick={ incrementSelf }>Increment Self</button>
      <button onClick={ decrementSelf }>Decrement Self</button>
      <p>Parent Count = {countFromParent}</p>
      <button onClick={ incrementParent }>Increment Parent</button>
      <button onClick={ decrementParent }>Decrement Parent</button>
    </div>
  )
}


class Parent extends React.Component {
  state = { count: 100 }
  
  increment = () => this.setState(oldState => ({ count: oldState.count + 1 }))
  decrement = () => this.setState(oldState => ({ count: oldState.count - 1 }))
  
  render() {
    return (
      <div id='Parent' className='box'>
        <h1>This is the parent.</h1>
        <p>This parent component has its own state and can manipulate it.</p>
        <p>Count = { this.state.count }</p>
        
        <button onClick={ this.increment }>Increment Self</button>
        <button onClick={ this.decrement }>Decrement Self</button>
        <Child1 countFromParent={ this.state.count } />
        <Child2 incrementParent={ this.increment } decrementParent={ this.decrement } />
        <Child3
          countFromParent={ this.state.count }
          incrementParent={ this.increment }
          decrementParent={ this.decrement }
        />
      </div>
    );
  }
}

ReactDOM.render(<Parent />, document.getElementById("root"))
              
            
!
999px

Console