When the defineModel macro is used in Vue.js, it does not automatically update the state

Click the link below for a sample of what I am attempting to do with Vue:

In my application, multiple parts of the data in storage need the same input format. So, I aim to make a generic input form that will take values from the parent component and establish a two-way connection between them, updating the main component's store. Since it is uncertain which value the input component manages at the input component level, altering the store directly in the child component isn't feasible.

However, when I follow the configuration provided in the example, the store doesn't update when the input values change. The reason behind this eludes me.

This appears to be due to Vue being unable to detect changes in the object. Reactivity is restored if v-model directives in the child are removed and replaced with :value and @input, followed by handlers that fully overwrite the model.value object. Refer to the link below for a functional demonstration. My comprehension might not be completely accurate, and there could be a way to achieve this using the simpler v-model.

Please find at the below link a minimal example of the type of thing I am trying to achieve in Vue.

Vue SFC Playground link

In my real application, I have several pieces of data maintained in my store which require the same format of input. Therefore, I want to create a general input form which will receive the values from the parent component, and will create a 2-way binding between them, updating the store in the main component. This setup means I cannot simply update the store in the child component, since I don't know at the input component's level which of the store's values the input component is managing.

However, when I set things up as in the example provided, the store is never updated when the values in the input changes. I cannot figure out why this is.

It seems this is something to do with Vue detecting changes in the object. If I remove the v-model directives in the child, and replace them with :value and @input, then use handlers which fully overwrite the model.value object, then reactivity is restored. See the below link for a working example. I'm just not sure my understanding is 100% solid, and if there's a way to make it work with the simpler v-model.

Vue SFC Playground link w/ working example

It is essential to ensure that your store remains reactive. To achieve this in your Store.js, follow these steps:

import { reactive} from 'vue'
const Store = reactive({
  name: {
    firstName: "Bill",
    lastName: "Gates"
  }
})
...

You need to make store reactive too, so in Your Store.js :

import { reactive} from 'vue'
const Store = reactive({
  name: {
    firstName: "Bill",
    lastName: "Gates"
  }
})
...

Answer №1

It is essential to ensure that your store remains reactive. To achieve this in your Store.js, follow these steps:

import { reactive} from 'vue'
const Store = reactive({
  name: {
    firstName: "Bill",
    lastName: "Gates"
  }
})
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Success callback for tracking conversions on Google

When an ajax call is successful, I am triggering the Google conversion tracking code. Along with tracking the conversion, I also need to change the window location. Is there a method to receive a callback when the conversion tracking is successful, so tha ...

Guide to updating text color in marquee tag with each reload

Seeking assistance in addressing the following issues... On each refresh, I want to change the text color within a marquee tag by utilizing a JavaScript function. I have obtained a color code like #18EEC5... however, when attempting to call the script fun ...

Using a dojo widget within a react component: A beginner's guide

Has anyone found a way to integrate components/widgets from another library into a react component successfully? For example: export default function App() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + ...

Creating custom modals for individual elements using PHP

Currently, I am working on a project that involves allowing users to select an item from a list retrieved from a MySQL database. The goal is to have buttons generated based on the selected item, shown below: https://i.sstatic.net/Oi5CG.png Here is a trim ...

Do we need to use aria-labelledby if we already have label and input associated with "for" and "id"?

Here is the HTML structure for the Text Field component from Adobe Spectrum: The <label> element has a for attribute and the <input> has an id which allows screen readers to read out the label when the input is focused. So, why is aria-label ...

Is there a way to effortlessly scroll an element when the CSS overflow property is designated as auto?

I need help with creating a testimonial section where two divs automatically scroll inside a fixed-height container. Currently, only one div is scrolling while the other remains static in my code. I have implemented JavaScript for automatic scrolling wit ...

Efficiently adjusting the height of a sticky sidebar

I am currently implementing a Bootstrap grid with two divs in a row. I need my reply-container to be fixed (sticky). However, when setting position: fixed; it is affecting the element's width by adding some additional width. With position: sticky, set ...

What is causing the Password Verification code to malfunction when used with a jquery form wizard?

I have implemented a jquery form wizard for my multi-page form. You can view an example of the form structure by visiting . Currently, the form utilizes a jquery validation plugin. However, I noticed that the jquery.validation.js file lacks validation rul ...

Recognize when the DOM undergoes modifications

After taking Matt's suggestion, I have made changes to the .ready() call. In my workflow, I utilize jQuery to set up certain configurations like this: $(function () { $('.myThing').each(function() { ... configuring happens he ...

Enhancing a popup with animated effects

I have been working on a popup that I want to add a subtle animation to. A fade effect seems like the perfect solution. Here is the code for the button: <a href="javascript:void(0)" onclick="document.getElementById('back_overlay').style.disp ...

What is the best way to display an AngularJS expression using a ternary operator?

Can AngularJS expressions be shown in a ternary operator? If so, can someone please provide guidance on how to achieve this? Thank you in advance. I have attempted the following, but it is not working as expected: {{ jdFile ? "Job Description File : {{fi ...

Error: The module "node_modules/react/index.js" does not export "default"

First time using Rollup to create a library and encountering an error with a basic button component when running rollup - c src/index.ts → dist/esm/index.js... [!] RollupError: "default" is not exported by "node_modules/react/index.js&qu ...

How can I access a nested document using Mongoose?

Suppose I have a data collection structured like this: [ { "_id": "637cbf94b4741277c3b53c6c", "text": "outter", "username": "test1", "address": [ { " ...

Encountered a problem while trying to install using npm install

Encountering an error while running npm install on Ubuntu 12.04: Here is a snippet from my npm-debug.log showing where the errors occur: ... (contents of npm-debug.log) ... This is my package.json: { "name": "www", "version": "0.0.1", /"p ...

Navigating with Link in React Router DOM v5 to successfully pass and catch data

In the process of developing a basic chat application, I encountered an issue with passing the username via the Link component. Below is the relevant code snippet: <Link to={{ pathname: `/${room}`, state: { the: user } }}> Enter room </Link> ...

Where will the user's input be stored?

Consider the following HTML code: <div class="form-group"> <label class="col-md-3 col-xs-3 col-sm-3 control-label">Phone</label> <div class="col-md-4 col-xs-4 col-sm-4"> <input id="input ...

Having trouble retrieving process variable in Vue3JS Vite project

In my Vue3 application, developed with Vite, I am integrating with a Solidity smart contract on Ropsten using web3js. Additionally, I am utilizing web3.storage to store images on IPFS, with the API key stored in a `.env` file at the project root: VUE_APP_A ...

How to improve page element hiding performance on Android and iOS using jQuery user agent detection?

I've recently started using GoNative, a service that helps me create iOS and Android apps from my website located at GoNative apps come with user agent detection capabilities, which I've explored in this article: To hide specific page elements ...

How to retrieve a value from a getter in a different file using Node.js

Recently, during the creation of a node project, I defined a model as follows: export default class Tokens { constructor() { this.accessToken = ''; this.refreshToken = ''; } getAccessToken() { return ...

Is there a way to prevent the omission of zeros at the end in JavaScript when using Number.toString(2)?

I am facing an issue while trying to reverse a 32-bit unsigned integer by converting it to a string first. The toString(2) function is causing the zeros at the end to be omitted, leading to incorrect output. This is my code: var reverseBits = function(n) ...