Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the vuex component isn't updated, despite the state updating?

When I load my page, I fetch a list of optboxes items.

Sources

Project's sources are online:

  • optboxes page ;
  • store (store, actions, mutations, getters).

optboxes pages

The HTTP request is well send and return adequate data:

created(){
    this.getOptboxes();
},
components: {
    'optbox': OptboxComponent,
},
methods: {
    getOptboxes() {
        optboxes.all().then((response) => {
            this.setOptboxes(response.data.output);
    }).catch(() = > {
            this.no_optbox_message = 'there is no optbox';
        logging.error(this.$t('optboxes.get.failed'))
    });
    }
},
vuex: {
    actions: { setOptboxes: actions.setOptboxes},
    getters: { optboxesList: getters.retrieveOptboxes}
}

I'm iterating over the results as follow:

<div v-for="optbox in optboxesList" class="panel panel-default">
     <optbox :optbox="optbox"></optbox>
</div>

Store

const state = {
    optboxes: {
        /*
        'akema': {
            hostname: "192.168.2.23",
            id: "akema",
            printers: [
                {
                    description: "bureau",
                    destination_port: 9100,
                    forward: "normal",
                    hostname: "1.2.3.4",
                    id: 0,
                    listening_port: 9102
                }
            ]
        }
        */
    }
};

Question

If I switch to another pages and come back then the list appear. I also notice that with the Vuex extension I can commit the state and see the changes.

Why are my changes not applied automatically?

like image 703
Édouard Lopez Avatar asked Dec 10 '22 15:12

Édouard Lopez


1 Answers

I had to change my data structure due to Change Detection Caveats.

Due to limitations of JavaScript, Vue.js cannot detect the following changes to an Array:

  • When you directly set an item with the index, e.g. vm.items[0] = {};
  • When you modify the length of the Array, e.g. vm.items.length = 0.

Store

optboxes is now an array.

const state = {
    optboxes:[]
}

Then update my mutations accordingly to edit the array.

like image 195
Édouard Lopez Avatar answered Feb 12 '23 10:02

Édouard Lopez