Vue: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "showIcon"

Created on 31 Jan 2018  ·  7Comments  ·  Source: vuejs/vue

Version

2.5.13

Reproduction link

https://codesandbox.io/s/728nx8mrm0

Steps to reproduce

pass .sync prop from parent to grandson through child

What is expected?

no warning

What is actually happening?

Most helpful comment

Sync across multiple layers is a bad practice anyway, unless for coupled components. If used in an app context, consider using a global state management solution.

All 7 comments

the role of the Child component is just a component wrapper or sth like that

You have to reemit in the Child that passes down a prop:

<grandson :showIcon="showIcon" @update:showIcon="v => $emit('update', v)></grandson>

Remember that sync is just syntax sugar

Sync across multiple layers is a bad practice anyway, unless for coupled components. If used in an app context, consider using a global state management solution.

@posva that make sense, is there any way to get rid of those things to make the wrapper more lighter weight, like react?

@yyx990803 大佬,建议在文档里说明一下啊。[手动狗头][手动狗头][手动狗头][无意冒犯]

What is a global state management solution?
image
I put it in an object.
parent:
<MyDateModePicker :date="date"></MyDateModePicker>
date: { D: '', W: '', M: '', mode: 'D' },
child component:
{{ date.mode }} {{ date }} {{ date.W }} <el-radio-group v-model="date.mode" size="small"> <el-radio-button label="D">日</el-radio-button> <el-radio-button label="W">周</el-radio-button> <el-radio-button label="M">月</el-radio-button> </el-radio-group>
It worked...

state management https://vuex.vuejs.org/
and it's normal that this is working for object as it is a non primitive type like array

Was this page helpful?
0 / 5 - 0 ratings