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

Watched Previous Values can't be Destructure on First Fire #418

Closed
klondikemarlen opened this issue Jun 30, 2020 · 7 comments · Fixed by #727
Closed

Watched Previous Values can't be Destructure on First Fire #418

klondikemarlen opened this issue Jun 30, 2020 · 7 comments · Fixed by #727
Assignees
Labels
misalignment Misalign with the latest RFC

Comments

@klondikemarlen
Copy link

When? watching an array of values I can't destructure the previous values on the initial loop run.
Why? the previous values are returned as undefined.
What I expect? I expect the previousValue on initial run to be equal to [undefined, undefined] so I can destructure it properly.

Code I have to write:

watch(
  () => [unref(unitId), unref(entityType)],
  ([newUnitId, newEntityType], oldValues) => {
    const [oldUnitId, oldEntityType] = oldValues || [undefined, undefined]

    if (!newUnitId || !newEntityType) return
    if (
      entitySchemaState.entitySchema &&
      newUnitId === oldUnitId &&
      newEntityType === oldEntityType
    )
      return

    fetch()
  },
  { immediate: true }
)

Code I want to write:

watch(
  () => [unref(unitId), unref(entityType)],
  ([newUnitId, newEntityType], [oldUnitId, oldEntityType]) => {
    if (!newUnitId || !newEntityType) return
    if (
      entitySchemaState.entitySchema &&
      newUnitId === oldUnitId &&
      newEntityType === oldEntityType
    )
      return

    fetch()
  },
  { immediate: true }
)
@antfu antfu added the typing label Jun 30, 2020
@antfu antfu self-assigned this Jun 30, 2020
@pikax
Copy link
Member

pikax commented Jul 3, 2020

That's the same behaviour as you have in vue3.

AFAIK this is the expected behaviour since you are deconstructing an undefined.

@antfu
Copy link
Member

antfu commented Jul 3, 2020

@klondikemarlen This should work for you

watch(
  [unitId, entityType],
  ([newUnitId, newEntityType], [oldUnitId, oldEntityType]) => {

  }
)

@antfu antfu removed the typing label Jul 3, 2020
@antfu
Copy link
Member

antfu commented Jul 4, 2020

Hope it solves your problem. Closing for now.

@antfu antfu closed this as completed Jul 4, 2020
@klondikemarlen
Copy link
Author

klondikemarlen commented Jul 4, 2020

I know that I can't destructure an undefined. :P

I want the watch function to always return an array if it is operating on an array of args.

@klondikemarlen
Copy link
Author

@klondikemarlen This should work for you

watch(
  [unitId, entityType],
  ([newUnitId, newEntityType], [oldUnitId, oldEntityType]) => {

  }
)

When I try passing in an array instead of a function, I get the same destructuring error.
image

@antfu antfu reopened this Jul 4, 2020
@antfu
Copy link
Member

antfu commented Jul 4, 2020

I will have a look

@antfu antfu added the misalignment Misalign with the latest RFC label Jul 9, 2020
@MrDenexi
Copy link

If this is expected behaviour, then I suggest updating the documentation to make it more clear. It is a bit confusing to get an error after using the 'Watching Multiple Sources'-example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
misalignment Misalign with the latest RFC
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants