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

Lifecycle hooks is executed in the wrong order. #8898

Closed
ipcjs opened this issue Aug 2, 2023 · 4 comments
Closed

Lifecycle hooks is executed in the wrong order. #8898

ipcjs opened this issue Aug 2, 2023 · 4 comments
Labels
🐞 bug Something isn't working 🔩 p2-edge-case

Comments

@ipcjs
Copy link

ipcjs commented Aug 2, 2023

Vue version

3.3.4

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-avbi9v?file=src%2FApp.vue

Steps to reproduce

  1. Click the open:0 button
  2. Click the close button
  3. Click the open:1 button

What is expected?

Console log:

open: 0
init             => onBeforeMount
onBeforeMount    => onMounted
onMounted        => onBeforeUnmount
onBeforeUnmount  => onUnmounted
close
init             => onBeforeMount
onBeforeMount    => onMounted
open: 1
init             => onBeforeMount
onBeforeMount    => onMounted
onMounted        => onBeforeUnmount
onBeforeUnmount  => onUnmounted

OR:

open: 0
close
init             => onBeforeMount
onBeforeMount    => onMounted
open: 1

What is actually happening?

Console log:

open: 0
init             => onBeforeMount
onBeforeMount    => onBeforeUnmount
onBeforeUnmount  => onMounted
onMounted        => onUnmounted
close
init             => onBeforeMount
onBeforeMount    => onMounted
open: 1
init             => onBeforeMount
onBeforeMount    => onBeforeUnmount
onBeforeUnmount  => onMounted
onMounted        => onUnmounted

System Info

System:
    OS: macOS 13.4.1
    CPU: (8) arm64 Apple M1
    Memory: 83.78 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.19.1 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.3 - /opt/homebrew/bin/npm
    pnpm: 8.4.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 115.0.5790.114
    Safari: 16.5.1

Any additional comments?

In this demo, Item's lifecycle hooks are sometimes executed in this order:
onBeforeMount->onBeforeUnmounted->onMounted->onUnmounted

@rocifier
Copy link

rocifier commented Aug 7, 2023

There is a unit test to ensure the order is correct. It can be found below:

it('lifecycle call order', async () => {

I'm uncertain how to reconcile your reproduction case with this. Something maybe wrong with your test case - I wouldn't expect onBeforeUnmount to be called at all when you click open.

@ipcjs
Copy link
Author

ipcjs commented Aug 10, 2023

The key point in reproducing the problem is effectiveVisible

Change modelRef.value?.effectiveVisible to visible.value can fix the demo:

const effectiveList = computed(() => list.value.filter(it => !visible.value || it !== selectedItem.value))

Console will print:

open: 0
close
init             => onBeforeMount
onBeforeMount    => onMounted
open: 1

@roojay520
Copy link

same problem: element-plus#15145

@edison1105
Copy link
Member

edison1105 commented Dec 14, 2023

Note: The Item.vue component was unmounted during the mounting phase. IMO, the onMounted hook should not be executed because the component was not actually successfully mounted. This is the same reason as #9264.
Maybe fixed by #9370
see demo base on #9370

@edison1105 edison1105 added the 🐞 bug Something isn't working label Dec 14, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐞 bug Something isn't working 🔩 p2-edge-case
Projects
None yet
Development

No branches or pull requests

5 participants