Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User's input is changing the <FormKit type="form">'s value prop #72

Closed
tosling opened this issue Feb 10, 2022 · 3 comments
Closed

User's input is changing the <FormKit type="form">'s value prop #72

tosling opened this issue Feb 10, 2022 · 3 comments
Assignees
Labels
🪄 enhancement New feature or request 🔵 priority-3 3. Lower priority issue 🚀 release-ready Feature or fix is complete and on an upcoming release branch
Milestone

Comments

@tosling
Copy link

tosling commented Feb 10, 2022

Describe the bug
When assigning a const obj = ref({}) to a <FormKit type="form"> element using the :value` prop, the component is making changes to the prop.

Generally in Vue, components should not mutate props, so that behavior comes unexpected.
(see. https://vuejs.org/guide/components/props.html#one-way-data-flow)

When objects and arrays are passed as props, while the child component cannot mutate the prop binding, it will be able to mutate the object or array's nested properties. This is because in JavaScript objects and arrays are passed by reference, and it is unreasonably expensive for Vue to prevent such mutations.

The main drawback of such mutations is that it allows the child component to affect parent state in a way that isn't obvious to the parent component, potentially making it more difficult to reason about the data flow in the future. As a best practice, you should avoid such mutations unless the parent and child are tightly coupled by design. In most cases, the child should emit an event to let the parent perform the mutation.

If - as a Developer - I want my data (outside of the component) being mutated, I can use v-model's two-way binding, which is available, too. https://formkit.com/essentials/forms#populating)

Reproduction

  1. Open Playground: https://formkit.link/d586e48ef957b35f08e95aad67c3b389
  2. The field is correctly pre-populated and also the debug output below
  3. Type something in the input
  4. Debug output changes, too, although v-model is not used.

Desktop (please complete the following information):

  • OS: Mac 11.6.2
  • Browser: Chrome 98.0.4758.80
@justin-schroeder justin-schroeder added this to the beta.3 milestone Feb 15, 2022
@justin-schroeder justin-schroeder added the 🪄 enhancement New feature or request label Feb 16, 2022
@justin-schroeder justin-schroeder self-assigned this Feb 16, 2022
@justin-schroeder justin-schroeder added the 🔵 priority-3 3. Lower priority issue label Feb 16, 2022
@justin-schroeder justin-schroeder added the 🚀 release-ready Feature or fix is complete and on an upcoming release branch label Feb 19, 2022
@justin-schroeder
Copy link
Member

In beta.3 we now clone the value prop before using it to prevent these kids of mutations.

@justin-schroeder
Copy link
Member

If you change the playground example to beta.3 you can see the non-mutated props now

justin-schroeder added a commit that referenced this issue Feb 20, 2022
@tosling
Copy link
Author

tosling commented Feb 23, 2022

Love it! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪄 enhancement New feature or request 🔵 priority-3 3. Lower priority issue 🚀 release-ready Feature or fix is complete and on an upcoming release branch
Projects
None yet
Development

No branches or pull requests

2 participants