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

              
                <p><label for="field">Change name to salute:</label> <input id="field" type="text" /></p>
<p>1: <button id="hello-1">Say too much hello <em>(warning, multiple alerts)</em></button></p>
<p>2: <button id="hello-2">Say hello correctly</button>
<p>3: <button id="hello-3">Always say hello to World</button></p>
<p>4: <button id="hello-4">Say too much hello <em>(warning, multiple alerts)</em></button></p>
<p>5: <button id="hello-5">Say hello correctly</button></p>
  
<p><em>These examples are here just to show how sideEffect works.
  In most of the cases, solution from button 2 will be enough.</em></p>
              
            
!

CSS

              
                
              
            
!

JS

              
                /*
  These examples are here just to show how sideEffect works.
  In most of the cases, solution from button 2 will be enough.
 */

$(function() {
  function reducer(state = 'World', action) {
    switch(action.type) {
      case 'set':
        return action.payload;
    }
    return state;
  }
  
  const store = Redux.createStore(reducer);
  
  // A simple function to get a sayHello function
  function getSayHello(name) {
    return function sayHello() {
      alert(`Hello ${name}`);
    };
  }
  
  // Be sure the field is always up to date with the store (useful for default value or if state changes outside this field)
  function renderField(value) {
    $(this).val(value);
  }
  
  // We attach a click listener at each render: the connected button will trigger multiple alerts
  function renderSayHello1(value) {
    const sayHello = getSayHello(value);
    $(this).click(sayHello);
  }
  
  // We remove the click listener before attaching a new one: the connected button will correctly say hello.
  function renderSayHello2(value) {
    const sayHello = getSayHello(value);
    $(this).off('click').click(sayHello);
  }
  
  // Calling sideEffect without dependencies makes the callback run only once: only the default state value will be alerted
  function renderSayHello3(value) {
    $(this).connect('sideEffect', function () {
      const sayHello = getSayHello(value);
      $(this).click(sayHello);
    });
  }
  
  // Calling sideEffect with one or more depencenies make the callback run when these dependencies change: here it'll be called each time the name changes. We attach a click listener at each render: the connected button will trigger multiple alerts like first button
  function renderSayHello4(value) {
    $(this).connect('sideEffect', function () {
      const sayHello = getSayHello(value);
      
      $(this).click(sayHello);
    }, value);
  }
  
  // sideEffect now returns a function. This function will be called before next render. It removes the click listener, therefore it'll work as expected, like the second button
  function renderSayHello5(value) {
    $(this).connect('sideEffect', function () {
      const sayHello = getSayHello(value);
      
      $(this).click(sayHello);
      
      return function() {
        $(this).off('click', sayHello);
      };
    }, value);
  }
 
  $('#hello-1').connect(renderSayHello1, store);
  $('#hello-2').connect(renderSayHello2, store);
  $('#hello-3').connect(renderSayHello3, store);
  $('#hello-4').connect(renderSayHello4, store);
   $('#hello-5').connect(renderSayHello5, store);
  
  $('#field')
    .connect(renderField, store)
    .change(function() {
      store.dispatch({ type: 'set', payload: $(this).val() });
    });
});

              
            
!
999px

Console