`onBeforeUnmount` weird behaviour if the the custom component is on the same DOM tree level.
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 54.4k
- Forks
- 9.2k
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 140
Description
Vue version
3.3.8
Link to minimal reproduction
https://stackblitz.com/edit/vue3-script-setup-with-vite-l1j4xb?file=src%2FApp.vue
Steps to reproduce
This problem could me my missing knowledge how vnodes and lifecycle hooks working under the hood, but this behaviour is a little bit odd for me because it's working differently with a fresh vite create app and on stackblitz but works differently on SFC Playground.
This is the behaviour on my local env (also on stackblitz):
https://github.com/mateenagy/vue-weird-rerender
- Create a new project with
npm create vite@latest my-vue-app -- --template vue-ts - Create a simple component with some prop and add
onBeforeUnmounthook with logging out the props.name for example. - Use the created component under each other like this:
<div>
<Test name="foo" />
<Test name="bar" />
<div>
- Open console in browser then delete the
name=foocomponent - Browser console will log
barinstead offoo
The behaviour in this is different because when I remove the first component then both onBeforeUnmount event triggered.
What is expected?
I leave some commented out example where it's working as expected:
When I remove the name=foo component then the onBeforeUnmount should log foo
What is actually happening?
It's actually trigger the last component onBeforeUnmount hook on the same DOM level:
<div>
<Test name="foo" /> <--- If I remove this
<Test name="bar" /> <--- This `onBeforeUnmount` will be triggered
</div>
This not happens in these cases:
<div>
<Test name="foo" /> <--- If I remove this then this component `onBeforeUnmount` hook will be trigged
<div></div>
<Test name="bar" />
</div>
<div>
<div>
<Test name="foo" /> <--- If I remove this then this component `onBeforeUnmount` hook will be trigged
</div>
<div>
<Test name="bar" />
</div>
</div>
<div>
<Test name="foo" v-if="show"/> <--- If I remove this then this component `onBeforeUnmount` hook will be trigged
<Test name="bar" />
</div>
System Info
No response
Any additional comments?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked StackBlitz reproduction and comparing it with the SFC Playground example, then inspect the component lifecycle behavior for adjacent components on the same DOM level. Done means removing the foo component invokes its own onBeforeUnmount hook and logs foo rather than bar.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100