Skip to content Skip to sidebar Skip to footer

Avoid Mutating A Prop Directly

I am getting warning: app.js:87486 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or c

Solution 1:

Initialise a data attribute from your prop, and manipulate that.

exportdefault {
    data() {
        return {
            numberOfLikes: this.likescount
        }
    },

    props: [
        'isliked',
        'path',
        'type',
        'likescount'
    ],

    methods:{
        like() {
            axios.post(`/${this.type}/${this.path}/like/`);

            // ...this.numberOfLikes++;
        },

        unlike() {
            axios.post(`/${this.type}/${this.path}/unlike/`);

            // ...this.numberOfLikes--;
        },
    }
}

Solution 2:

Just as the warning says you shall not mutate props, remove this.likescount ++; this.likescount --;

and this will remove the warning ... your props should be pure

Post a Comment for "Avoid Mutating A Prop Directly"