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

injected ref value is not reactive when updated inside setup of a child component #2043

Closed
amrnn90 opened this issue Sep 3, 2020 · 5 comments

Comments

@amrnn90
Copy link

amrnn90 commented Sep 3, 2020

Version

3.0.0-rc.10

Reproduction link

https://codesandbox.io/s/admiring-snow-4u748?file=/src/Child.vue

Steps to reproduce

Open the reproduction link, in parent component setup we have:

 setup() {
    const msg = ref("initial");
    provide("CONTEXT", { msg });

    return {
      msg
    };
  }

and in a child component setup:

  setup() {
    const ctx = inject("CONTEXT");

    // not working (works when wrapped inside onBeforeMount)
    ctx.msg.value = "updated";
  }

What is expected?

the screen should show: "updated"

What is actually happening?

the screen shows "initial"


It works fine when wrapped inside a lifecycle hook:

onBeforeMount(() => {
    ctx.msg.value = "updated";
})

I'm not sure if it's a bug or intentional behavior, but I find it weird since the parent's setup function is executed before the child's.

@Picknight
Copy link
Contributor

The usage here is different from the document. 🧐

However, there are times where we need to update the data inside of the component where the data is injected. In this scenario, we recommend providing a method that is responsible for mutating the reactive property.

@amrnn90
Copy link
Author

amrnn90 commented Sep 4, 2020

@Picknight
providing a method that updates the ref and calling it in the child's setup makes no difference.
https://codesandbox.io/s/elated-meadow-6r04w?file=/src/Child.vue

@Picknight
Copy link
Contributor

The deep reason is ac81dcf. @yyx990803 Maybe we should revert it ?

@zarub2k
Copy link

zarub2k commented Jun 29, 2021

I see this issue is closed but I am not seeing any conclusion on this thread. Am I missing something? I am also looking for solution to my problem.

@thexeos
Copy link

thexeos commented Jul 11, 2021

I'm trying to inject a Boolean value into all components (SFC), which provide/inject seems best suited for. I have Vue 3.1.2.

I tried calling:

const injectedRef = ref(getCurrentState())
app.provide('injectedName', injectedRef)

UZu7neyjaw

const injectedComputed = computed(() => injectedRef)
app.provide('injectedName', injectedComputed)

9JUlnMXIWO

const injectedReactive = reactive({ injectedRef })
app.provide('injectedName', injectedReactive)

zWJglrBvjm

As you can see, only the last method worked, but that defeats the purpose of injecting a single true/false variable that can be used directly in components.

This was previously done with:

const reactiveProxy = Vue.observable({ state: getCurrentState() })

Vue.use({
  install (vueInstance) {
    Object.defineProperty(vueInstance.prototype, '$injectedName', {
      get: () => reactiveProxy.state
    })
  }
})

@github-actions github-actions bot locked and limited conversation to collaborators Oct 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants