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 src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>

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

CSS

              
                body {
  height: 100vh;
  display: grid;
  place-items: center;
}

.box {
  width: 300px;
  
  h1 {
    font-size: 20px;
  }
}

p {
  margin-bottom: 1em;
}

              
            
!

JS

              
                // https://siriusbusiness.fi/gun-for-react-state-management

// Initialize Gun with options that make sure the state is synced to localStorage only
const State = new Gun({
    localStorage: true,
    file: 'State.local', 
    multicast: false,
    peers: []
}).get('state');

class BaseComponent extends React.Component {
    subscriptions = {};
    
    subscribe(callback, path) {
        return (value, key, x, subscription, f) => {
            if (this.unmounted) {
                subscription && subscription.off();
                return;
            }
            this.subscriptions[path || key] = subscription;
            callback(value, key, x, subscription, f);
        }
    }
    
    inject(name, path) {
        return this.subscribe((v,k) => {
            const newState = {};
            newState[name || k] = v;
            this.setState(newState);
        }, path);
    }
    
    componentWillUnmount() {
        this.unmounted = true;
        Object.keys(this.subscriptions).forEach(k => {
            const subscription = this.subscriptions[k];
            subscription && subscription.off();
            delete this.subscriptions[k];
        });
    }
}

class TextInput extends BaseComponent {
    state = {text:''};
  
    componentDidMount() {
        State.get('text').on(this.inject());
    }
    
    onInput(e) {
        State.get('text').put(e.target.value);
    }
    
    render() {
        return (
            <form class="box">
                <input placeholder="Type something" type="text" value={this.state.text} onChange={e => this.onInput(e)} />
            </form>
        );
    }
}

class TextInputList extends BaseComponent {
  state = {elementCount: 2}
  
  componentDidMount() {
    State.get('elementCount').on(this.inject());
  }
  
  render() {
    return (
      <>
        <p>
          <button onClick={() => State.get('elementCount').put(Math.max(this.state.elementCount - 1, 0))}>Remove element</button>
          <button onClick={() => State.get('elementCount').put(this.state.elementCount + 1)}>Add element</button>
        </p>
        {[...Array(this.state.elementCount)].map((e, i) => <TextInput key={i} />)}
      </>
    );
  }
}

const el = document.querySelector("#root");
ReactDOM.render(<>
    <p class="box">The input value is synced across component instances and persisted in localStorage. State changes are properly unsubscribed on unmount.</p>
    <TextInputList />
    </>, el);
              
            
!
999px

Console