0
<template>
...
<note-info></note-info>
<note-list></note-list>
...
</template>

I using VueX, store.js

export default {
    state: {
        noteDataList: [],
    },
    mutations: {
        setNoteDataList: function (state, payload) {
            state.noteDataList = payload;
        },
    }
};

Note List Component:

...
created() {
   const note_list = [{id: 1, name: "Monday"},{id: 2, name: "Tuesday"}]
   this.$store.commit("setNoteDataList", note_list);
}
...

Note Info Component:

...
computed: {
   ...mapState(['noteDataList'])
},
mounted() {
   console.log(this.$store.state.noteDataList[0]);
},
...

Note info component can't get first object from note list

1 Answer 1

1

There's race condition, the specified order of execution cannot be expected from sibling components. They are potentially instantiated in the order they appear in a template, but this isn't guaranteed and shouldn't be relied on.

The logic that is responsible for displaying child components (setNoteDataList, etc) can be moved to parent component, especially if it's synchronous.

Alternatively, the component that accesses noteDataList shouldn't expect it to be ready on instantiation and needs to treat is as reactive value via computed properties or watchers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.