cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <h1>Vanilla JS Redux example</h1>
<div class="container">
  <button id="increaseCounterButton"> + (0) </button>
  <span id="countLabel"></span>
  <button id="decreaseCounterButton"> - (0) </button>
</div>
            
          
!
            
              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;
}
            
          
!
            
              
/****** 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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console