Skip to content

Props able to edit parent object value (One-Way Data Flow Violence) #13816

@ramamimu

Description

@ramamimu

Vue version

3.5.13

Link to minimal reproduction

https://play.vuejs.org/#eNp9U8GO2jAQ/ZWRL4DEJqraEwWkdrVSW2m7qO3Rl5AMYJrYlu2kSFH+vWM7CdDuRiDFnnnz/Gb83LJPWidNjWzF1jY3Qjuw6Gq95VJUWhkHLRg8QAcHoyqYEXQ2ph5Vpft4kvqNZ5p95JLLXEnroLLHl/0ZNp5i3nIJPrICzugnuwWX6zQeSsfRxmGly8wh7QDWp3fbXWZQunVKyyHUtj1rQh/oumtSSF07aB4qVWC54ewK4wzSiAmKVzrQPlBmhHHmEev0RgJbMmepj4M4JmerJI0otMBZTiyiRPOinaA+OVtByPhcVpbqz7cQc6bG5RDPT5j/fiV+thcf42xn0KJpkLMx5zJzRBfTTz+/44XWY5LarEtCTyR/oFVl7TVG2OdaFiT7BhfUfg23KeTxl326OJR2aMoL9cgu4Dmj2/UDfKv1q9z3yYdQR5dMUxycMeUxJZ9VLR0Wd04jK024cHCZNkpbMlmBByFx53dzf3wvLN72szceXTTmrpdG9hsY8pMoi2mnjgLn8wVstpF8qEuarKyRqoOUZDwxlk7bvBBNWER7P3rG0dMAX8QS3ElYoH84Dbz5lCT+JQga5EnVFJTKQbYvEZwCLCgeJfS8exPtP8wCoty5JHAl3GJFEHpYo2z/qvaDqGvxzTJKCTR98TCKWAv/F//7PMfZ3T3QNI7j/iF2fwH4r4wU

Steps to reproduce

  1. Fill the child component's input form with any sentences, it will edit the parent as well

What is expected?

The parent component value should not be edited because there is no emit or anything else that can update parent object value. This violence One Way Dataflow - child able to edit parent only from a Props

What is actually happening?

When the parent pass object to props then the child component receive and connect it to a variable using onMounted method, this make the parent also edited when child did.

const props = defineProps(
  {
    parentMsg: Object
  }
)

const childObj = ref({
  msg: ""
})

onMounted(() => {
  // parent will be editted as well even there is no emit
  childObj.value = props.parentMsg
})

System Info

Any additional comments?

for now, to avoid this issue. I connect the variable with object spread operator

onMounted(() => {
    childObj.value = {...props.parentObj};
});

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions