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

Watch of props not activated #1024

Open
vallemar opened this issue Jan 4, 2023 · 7 comments
Open

Watch of props not activated #1024

vallemar opened this issue Jan 4, 2023 · 7 comments
Labels
V3 Issues related to V3

Comments

@vallemar
Copy link
Contributor

vallemar commented Jan 4, 2023

When you declare a wath of a prop in a child component the event is not fired: Preview NS, Playground Vue3. You can see how the Details.vue watch is fired in vue but in NS-vue the event is never fired

@heywhy
Copy link
Contributor

heywhy commented Jan 22, 2023

I can confirm that using defineComponent works instead. See, Preview NS. I'm not sure why the variant of SFC is not working 🤔

@heywhy
Copy link
Contributor

heywhy commented Jan 22, 2023

Not destructuring the props actually works;

import { watch } from 'nativescript-vue';

const props = defineProps({
  text: String,
});

watch(
  () => props.text,
  () => {
    console.log("This watch should activate but it doesn't", props.text);
  }
);

@vallemar
Copy link
Contributor Author

@heywhy I have noticed this in some components, I have some others that do not work like this either, I do not understand why in some it does and in others it does not

@rigor789 rigor789 added the V3 Issues related to V3 label Mar 1, 2023
@igorjacauna
Copy link

In Vue 3 docs says:

Note that if you destructure the props object, the destructured variables will lose reactivity. It is therefore recommended to always access props in the form of props.xxx.

You can use toRefs if really need to destructure:

If you really need to destructure the props, or need to pass a prop into an external function while retaining reactivity, you can do so with the toRefs() and toRef() utility APIs:

const props = defineProps({
  text: String,
});

const { text } = toRefs(props)

watch(
  () =>text,
  () => {
    console.log("This watch should activate, text);
  }
);

@MykolaZhaivoroniuk
Copy link

MykolaZhaivoroniuk commented Sep 10, 2023

Hey @igorjacauna .
I tried with you code.
Bu it isn't works too.
I would like you to give me an advice.

This is my code.

`<script setup lang="ts">
import { ref, watch, onMounted, toRefs } from "vue";

const props = defineProps({
tabs: { default: [], type: Array },
tabIndex: { default: 0, type: Number },
});
const { tabs, tabIndex } = toRefs(props);

watch(
() => tabIndex,
() => {
console.log(tabIndex);
}
);
</script>

This is the tabIndex: {{ tabIndex }}

`

There isn't any output.
But the tabIndex is changed of the p tag.

@meghraj-suthar
Copy link

I'm also facing this same issue. watch is not being called even though props are getting changed and the latest value is being displayed in the UI but not calling watch.

@vallemar
Copy link
Contributor Author

@meghraj-suthar can you show your code?

It seems like the problem is decomposing the props, but it's not an NS-Vue problem, it seems to be vue's behavior. Check this comment #1024 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
V3 Issues related to V3
Development

No branches or pull requests

6 participants