question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue 2 - input value is not updated in some cases

See original GitHub issue

Vue.js version

2.0.0.rc.7 and older

Reproduction Link

https://jsfiddle.net/Lonzpg7h/

Steps to reproduce

  1. Type one letter into input field - value won’t show up but console logs that event was fired
  2. Type more text - you will see now that value is updating correctly
  3. Now repeat step 1 but with removed this.isDirty = value !== '' or 'is-dirty': this.isDirty declaration - value shows up immediately

What is Expected?

I think input value should be visible on the first keystroke as well

What is actually happening?

In some case input value doesn’t show up until you enter text for the second time.

Any idea what’s going on here?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

11reactions
LinusBorgcommented, Sep 27, 2016

So what happens here is this:

  • onInput is triggered for the first time
  • this changes isDirty from true to false
  • this leads to a re-render of the component because isDirty is used in the render function
  • since the prop this.value is still an empty string '', the content of the input is reset.
  • subsequent calls to oInput don’t lead to a re-render because this.isDirty is already true

This only happens because you don’t $emit the event. If you do,

  • the parent gets the value
  • that triggers an update of the value prop in the child
  • that would lead to the new value being inserted in the input upon that first re-render.

…and everything would be fine. Your example broke the cycle, so to speak.

Edit: ah, Centaur beat me to it.

9reactions
Centaurcommented, Sep 27, 2016

@sqal

In vue 2.0,

<textfield v-model="text"></textfield>

means if you don’t fire input event from textfield, the value prop of textfield will always remain the initial value of text which is ''.

Since you never fire any input event, so value prop never changes, so the only thing that can trigger the re-render of textfield is the isDirty state.

When you input a char for the first time, the char appears first, this is the native behavior of input tag. Then this input causes the change of isDirty from false to true, which in turn causes the re-render of textfield, notice that it’s value is still empty, so the content of the input gets cleared.

For the subsequent char inputs, the native behavior of input still takes effect so you can see them as well. But this time, isDirty remains true, so no re-render occurred on textfield so the content is not cleared.

If this.isDirty = value !== '' is removed, even the first re-render does not occur because isDirty has not changed, so you can see what you have input immediately, that is just the consequence of textfield not being re-rendered.

I modified your code into a template-based one to make things clearer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Input field's binded value does not update in Vue
I am expecting that binded input field value will change according respective variable - if I don't misunderstand the documentation, it's a ......
Read more >
Input is not updating value when using created() option and v ...
I'm looping through config object inside created() hook since the Vue object doesn't seem to be available in my component at this time....
Read more >
Adding a new todo form: Vue events, methods, and models
For installation, and to use some of the more advanced features of Vue (like Single File Components or render functions), you'll need a...
Read more >
Updating unrelated Vue.js variable causing input values in ...
When a user types into one of your <input> elements you aren't storing that value anywhere. You've got a :value to poke the...
Read more >
v-model not updating when data changes in edit form
If you are using the Vue dev tools extension for Chrome, you may want to investigate what's the updateId , updateJurisdictionName and updateJurisdictionStatus...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found