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

              
                <h1>Vanilla JS Redux example</h1>
<div class="container">
  <button id="increaseCounterButton"> + (0) </button>
  <span id="countLabel"></span>
  <button id="decreaseCounterButton"> - (0) </button>
</div>
              
            
!

CSS

              
                h1{
  text-align: center;
}

.container{
  display:flex;
  width:100%;
  justify-content: center;
  align-items:center;
}

#countLabel{
  margin: 40px;
  font-size: 2rem;
}

button{
  display: inline-block;
  width: 80px;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  border: 1px solid transparent;
  padding: .375rem .75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: .25rem;
  transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
  color: #fff;
  background-color: #007bff;
  border-color: #007bff;
}
              
            
!

JS

              
                
/****** the example starts here ******/

//actions
const INCREASE_COUNTER = 'INCREASE_COUNTER';
const DECREASE_COUNTER = 'DECREASE_COUNTER';

function increaseCounter(amount){
  return {
    type: INCREASE_COUNTER,
    payload: amount
  }
}

function decreaseCounter(amount){
  return {
    type: DECREASE_COUNTER,
    payload: amount
  }
}


//reducers
function counterReducer(state={count:0, increaseClicks:0, decreaseClicks:0}, action){
  switch(action.type){
      case INCREASE_COUNTER:
        return {
          ...state, 
          count: state.count + action.payload,
          increaseClicks: state.increaseClicks + 1
        };
      case DECREASE_COUNTER:
        return {
          ...state, 
          count: state.count - action.payload,
          decreaseClicks: state.decreaseClicks + 1          
        };      
    default:
       return state;
  }
}


//codepen equivalent to import { createStore } from 'redux'
const { createStore } = Redux; 
//declare store
let store = createStore(counterReducer);


//reference UI elements
const increaseCounterButton = document.getElementById('increaseCounterButton');
const decreaseCounterButton = document.getElementById('decreaseCounterButton');
const countLabel = document.getElementById('countLabel');


//dispatch actions on buttons click
increaseCounterButton.addEventListener('click', ()=>{
  store.dispatch(increaseCounter(1));
});
decreaseCounterButton.addEventListener('click', ()=>{
  store.dispatch(decreaseCounter(1));
});


//render cycle that gets called every time the store is modified
const render = () => {
  //get current state
  const state = store.getState();
  //update UI based on current state
  countLabel.innerHTML = state.count;
  increaseCounterButton.innerHTML = `+ (${state.increaseClicks})`;
  decreaseCounterButton.innerHTML = `+ (${state.decreaseClicks})`;   
}

render();
store.subscribe(render);

              
            
!
999px

Console