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

refs are not available on $nextTick when using portal #23

Closed
sunabozu opened this issue Mar 16, 2017 · 5 comments
Closed

refs are not available on $nextTick when using portal #23

sunabozu opened this issue Mar 16, 2017 · 5 comments
Assignees
Milestone

Comments

@sunabozu
Copy link

Hi.

This is not very standard issue, but it's definitely caused by portal-vue. Let's see the code:

<portal to="whatever">
  <div v-if="open" ref="div">
  ...
  </div>
</portal>
methods: {
  handler(e) {
    this.open = true

    this.$nextTick(() => {
      const rect = this.$refs.div.getBoundingClientRect()
  }
}

If I click a button somewhere and trigger my handler, I get an error, because this.$refs.div is undefined. The element inside the portal isn't rendered yet at this point. But when I do the same without the portal, it works as intended. So far I couldn't come up with an adequate solution. Any ideas?

@LinusBorg
Copy link
Owner

I'll have a look at this, but I'm not sure I can find a proper solution.

The reason for this beahviour is that I postpone the rendering update of any portal-target with a setTimeout(..., 0). I do because I have to prevent race conditions as best I can when portal targets or sources are switched dynamically, and to do so I collect all updates, then clean old contents and only after that insert all new contents.

This timeout is what produces your behaviour.

I'll try and find a solution, but can't promise anything yet.

@LinusBorg LinusBorg self-assigned this Apr 10, 2017
@LinusBorg LinusBorg added this to the 1.0.0 milestone May 8, 2017
@LinusBorg
Copy link
Owner

LinusBorg commented May 11, 2017

Hm, so I switched the mechanism for my async scheduler to use $nextTick, but that doesn't help.

I have ideas about how to make the wormhole updates happen synchronously, but that might technically be a breaking change, but only a little one.

Anyways and I won't attempt to make this change this before #26 is done, so this will move to the 1.1 Milestone for now.

@LinusBorg LinusBorg modified the milestones: 1.1.0, 1.0.0 May 11, 2017
LinusBorg added a commit that referenced this issue May 14, 2017
* develop: (27 commits)
  1.0.0-beta.4
  chores
  changed build script to work around cross-env
  improved examples, added router to split things up nicely
  fix example index.js for production run
  new test build
  bumping dependencies
  caused troubles with dependency svgo
  fix dockute dependency and nom script
  add $refs test to the examples
  add section about $refs caveat from #23 to docs
  chores
  clean up npm scripts, include test run in build script
  remove unnecessary folder
  add „Caveats“ page to docs
  add test for abstract component option
  Document SSR limitations when using „targetEl“
  make „to“ optional when targetEl is provided (fix #30)
  add reference to parent component (fix #34) (fix#35) (+1 squashed commit) Squashed commits: [770211d] enhance `targetEl` example with test component to verify that it shows up in devtools correctly.
  switch to nextTick instead of timeout in hopes of making refs work
  ...
@LinusBorg LinusBorg changed the title ref and v-if inside the portal refs are not available on $nextTick when using portal May 17, 2017
@LinusBorg
Copy link
Owner

I will close this in favor of a real feature issue #39

@bimbiltu
Copy link

bimbiltu commented Apr 5, 2018

Hey @LinusBorg I may be doing something wrong but I dont think this was ever solved with #39: https://jsfiddle.net/ke9b8j4e/3/ (i inlined portal-vue so youll need to scroll to the bottom of the script section to see the app code)

I found that if I use a second $nextTick then the $ref is populated as expected

@LinusBorg
Copy link
Owner

Please open a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants