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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              // The Provider component will be our root component
// It connects our app to the store
const {Provider} = ReactRedux;

// The App component is the root of our app itself.
var App = (props) => 
  <div>
    <h1>Tomb of the Ancients</h1>
    <HeroContainer></HeroContainer>
  </div>

// Hero is a presentational (dumb) component 
// that recieves a hero as a prop and renders it
var Hero = (props) => 
  <div className="hero">
    <h2>{props.hero.name}</h2>
    <pre>
      {JSON.stringify(props, null, 2)}
    </pre>
    <button onClick={
      () => store.dispatch({
        type:"DAMAGE_HERO", 
        value: 1
      })
    }>
      Damage
    </button>
    <button onClick={
      () => store.dispatch({
        type:"HEAL_HERO", 
        value: 10
      })
    }>
      Heal
    </button>
  </div>

// A container (smart) component, that gets heros 
// from the store, and passs them as props to the 
// Heros component
var HeroContainer = ReactRedux.connect(
  (state) => ({hero: state.hero})
)(Hero);

// The initial state for the app
const initialState = {
  hero: {
    name: "Haldane Goodguy", 
    type: "Swordsman", 
    maxHp: 36, 
    hp: 36, 
    armour: 33, 
    attack:12
  },
  monster: {
    name: "Vlad the Bad", 
    type: "Necromancer", 
    maxHp: 300, 
    hp: 300, 
    armour: 28, 
    attack:2
  }
}

/* The Hero Reducer only has to deal with heros */
const heroReducer = (hero=initialState.hero, action) => {
  switch (action.type) {
    case "DAMAGE_HERO":
      return { ...hero, hp:hero.hp - action.value } 
    case "HEAL_HERO": {
      let newHero = {...hero, hp: hero.hp + action.value }
      if (hero.hp > hero.maxHp) { hero.hp = hero.maxHp }
      return newHero
    }
    default:
      return hero    
  }
}

/* The Hero Reducer only has to deal with heros */
const monsterReducer = (monster=initialState.monster, action) => {
  switch (action.type) {
    case "DAMAGE_MONSTER":
      return { ...monster, hp:monster.hp - action.value }
    case "MONSTER_OVERDRIVE": 
      return { ...monster, attack:500, armour:1000 }
    default:
      return monster    
  }
}

/* We create a new reducer by combining other reducers */
const reducer = Redux.combineReducers({
  hero:heroReducer,
  monster:monsterReducer
});

const store = Redux.createStore(reducer);

store.subscribe(() =>
  console.log(store.getState())
)

// setInterval(() => store.dispatch({type:"SET_NAME", value: "Dave"}), 1000);

// setInterval(() => store.dispatch({type:"SET_NAME", value: "Mike"}), 1300);

ReactDOM.render(
  <Provider store={store}>
    <App></App>
 </Provider>, 
  document.getElementById('app'))

            
          
!
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.

Console