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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 id="app">
  <section class="section">
    <div class="content">
  <h1 class="title">Vuex Reactivity Caveats</h1>
<p class="content">
Vuex shares the same reactivity caveats regarding property addition as Vue.  This CodePen shows how to use <code>Vue.set</code> to add a reactive property to a Vuex store.  It also presents two failed attempts that I've seen while helping on the Vue Land Discord.
</p>
<p class="content">
To test the code, select one of the following radio buttons.  Each selection triggers different versions of the ADD_PROPERTY mutation.
</p>    
    <div class="control content">
  <label class="radio" for="vueSet">
    <input type="radio" id="vueSet" value="vueSet" v-model="demoSelection">
    Use Vue.set
  </label>
  <label class="radio" for="directAssignment" >
    <input type="radio" id="directAssignment" value="directAssignment" v-model="demoSelection">
    Direct assignment
  </label>
        <label class="radio" for="replaceItem">
    <input type="radio" id="replaceItem" value="replaceItem" v-model="demoSelection">
    Replace array item
  </label>
</div>
    <p class="content">
      Only the selection for "Use Vue.set" will allow the property to be added reactively.  This means that when you press the Add Property button after making a selection, you'll see the UI update immediately.  The other selections won't show the new property or it will only show up later after additional selections are made.
              </p>
    <hr />
    <div class="card">
      <div class="card-header">
        <p class="card-header-title">
          Demonstrating&nbsp; 
          <span v-if="demoSelection == 'vueSet'">Vue.set</span>
          <span class="has-text-danger" v-else-if="demoSelection == 'directAssignment'">Direct Assignment (Won't Work)</span>
          <span class="has-text-danger" v-else>Replace Array Item (Won't Work)</span>
        </p>
      </div>
      <div class="card-content">
    <div class="content">
      <p>
        Press the Select buttons to navigate through different items.  Press De-Select to clear the selection.
      </p>
      <button v-for="item in $store.state.items" :key="item.id" class="button" @click="makeSelection(item.id)">Select Item #{{ item.id }}</button>
<button class="button" @click="makeSelection(-1)">De-Select Item</button>
  </div>
    <h2>Item Selected</h2>
    <p v-if="!$store.getters.selectedItem">No Item Selected</p>
    <div v-else>
      <p>{{ $store.getters.selectedItem.id }}</p>
      <p v-if="$store.getters.selectedItem.obj">Property obj: {{ $store.getters.selectedItem.obj }}</p>         
      <div v-else>
        <p>Property Not Added</p>
        <button class="button is-primary" @click="addProperty">Add An Object Property</button>
  </div>
      </div>
        </div>
    </div>
    </div>
    </div>
</section>   
<section class="section">
    <footer class="footer">
    References: <a href="https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats">Vue Docs</a> and <a href="https://vuex.vuejs.org/guide/mutations.html">Vuex Docs</a>
  </footer>
</section>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const store = new Vuex.Store({
  state: {
    items: [
      { id: 1 },
      { id: 2 }
    ],
    selectedId: -1
  },
  mutations: {
    SET_SELECTED_ID(state, id) {
      state.selectedId = id;
    },
    SET_ITEMS(state, items) {
      state.items = items;
    },
    ADD_PROPERTY_1: state => {
      let itm = state.items.find( itm => itm.id == state.selectedId );
      if( itm ) {
        Vue.set(itm, "obj", { "data": "Hello"});
        // THIS WON'T BE REACTIVE 
        //itm.obj = { "data": "Hi" };
      }
    },
    ADD_PROPERTY_2: state => {
      let itm = state.items.find( itm => itm.id == state.selectedId );
      if( itm ) {
        // THIS WON'T BE REACTIVE 
        itm.obj = { "data": "Hi" };
      }
    },
    ADD_PROPERTY_3: state => {
      // this won't work
      let idx = state.items.findIndex( itm => itm.id == state.selectedId );
      if( idx >= 0 ) {
        state.items[idx] = { ...state.items[idx], "data": "myvalue" };
      }
    }
  },
  getters: {
    selectedItem: state => state.items.find( itm => itm.id == state.selectedId )
  }
});

new Vue({
  el: "#app",
  store,
  watch: {
    demoSelection(val) {
      this.$store.commit("SET_SELECTED_ID", -1);
      this.$store.commit("SET_ITEMS", [
      { id: 1 },
      { id: 2 }
    ]);
    }
  },
  methods: {
    makeSelection(id) {
      this.$store.commit("SET_SELECTED_ID", id);
    },
    addProperty() {
      if( this.demoSelection == "vueSet" ) {
        this.$store.commit("ADD_PROPERTY_1");
      } else if( this.demoSelection == "directAssignment" ) {
        this.$store.commit("ADD_PROPERTY_2");
      } else {
        this.$store.commit("ADD_PROPERTY_3");
      }
    }
  },
  data: {
    demoSelection: "vueSet"
  }
});
              
            
!
999px

Console