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

              
                <div class="container"></div>
              
            
!

CSS

              
                .container{
  padding: 20px;
}

.form-inline{
  input{
    display: inline-block;
    width: auto;
    margin: 5px;
  }
}

.app{
  div{
    margin: 50px 0px;
  }
}

.counter-component{
  button{
    margin: 0px 5px;
  }
}

              
            
!

JS

              
                /* kind of imports from codepen.io global vars */
const { createStore, applyMiddleware, combineReducers } = Redux;
const { Provider, connect } = reactRedux;


/*
 * ACTIONS
 */
const INCREASE_ACTION = 'INCREASE_ACTION';
const DECREASE_ACTION = 'DECREASE_ACTION';

/* increase action creator: payload = +1*/
const increaseActionCreator = () =>{
  console.log("increased called");
  return{
    type: INCREASE_ACTION,
    payload: 1
  }
}

/* decrease action creator: payload = +-1*/
const decreaseActionCreator = () =>{
  return{
    type: DECREASE_ACTION,
    payload: -1
  }
}


/*
 * REDUCERS
 */

/* if INCREASE/DECREASE ACTION detected, modify state as defined in payload*/
const counterReducer = (state=0, action) => {
  switch (action.type){
    case INCREASE_ACTION:
    case DECREASE_ACTION:
      return state + action.payload; 
  }
  
  return state;
};

/* assign counterReducer result to state.counter*/
const rootReducer = combineReducers({
  counter: counterReducer
});



/*
 * COUNTER COMPONENT
 */

const CounterComponent = (props) => {
    return (
      <div className="counter-component"> 
        <h2>Superstitious counter component</h2>
        <label>Counter: {props.counter}</label>
        <button type="submit" className="btn btn-default"
          onClick={props.increaseActionCreator}> + </button>        
        <button type="submit" className="btn btn-default"
          onClick={props.decreaseActionCreator}> - </button>
      </div>
    ) 
}
/* INCOMING DIRECTION: map redux state.counter to component props.counter*/
function mapStateToProps(state){
  return {counter: state.counter};
}
/* USE connect HOC to decorate CounterComponent with redux state and action creators*/
const ReduxCounterComponent = connect(mapStateToProps, {increaseActionCreator, decreaseActionCreator})(CounterComponent);


/*
 * APP (MAIN) COMPONENT
 * simply renders ReduxCounterComponent
 */

class App extends React.Component {
  render() {
    return (
      <div className="app">
        <ReduxCounterComponent />
      </div>
    );
  }
}




/*
 * MIDDLEWARE
 */

/*log called actions*/
const logger = ({ dispatch }) =>{
    return next => action => {
        console.log("ACTION CALLLED: ", action.type, " with payload: ", action.payload);
        next(action);
    }
}



/*prevent to have a counter with value 13!!*/
const superstitiousMode = (store) => {
    return next => action => {
        if(store.getState().counter + action.payload == 13 ){
            const newAction = {...action, payload:action.payload*2}
            store.dispatch(newAction);
        }
        else{
            next(action);
        }
    }
}


/*
 * INDEX.JSX
 *  - create redux store with 'logger' middleware
 *  - assign store to Provider
 *  - render App (main) component in index.html ".container" element
 */

const createStoreWithMiddleware = applyMiddleware(logger, superstitiousMode)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(rootReducer)}>
    <App />
  </Provider>
  , document.querySelector('.container'));


              
            
!
999px

Console