question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useState not working with v-if when value set in watch statement

See original GitHub issue

Environment

Nuxt project info: 17:40:50


  • Operating System: Linux
  • Node Version: v16.16.0
  • Nuxt Version: 3.0.0
  • Nitro Version: 1.0.0
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: telemetry, runtimeConfig, css, build, vite
  • Runtime Modules: -
  • Build Modules: -

Reproduction

use useFetch then watch, in watch affet a boolean in a useState command, then in a component try to use the value in v-if statement => note working

index.vue (or any name)

<template>
	<div>
		<button @click='onSubmit'>CLICK</button>
	</div>

</template>

<script lang="ts" setup>

	function onSubmit() {

		const {data: status} = useFetch('https://httpbin.org/get')

		watch(status, (newStatus) => {

			console.log('call ok')

				if(newStatus){
					useState('test', ()=>'test');
                                        useState('isConnected', ()=>true);
				}
			
			},{
				deep: true,
				immediate:true
			}
		)

}

</script>

in component (Menu.vue)

<template>
	<div>
				<NuxtLink to="/" src>
					HOME
				</NuxtLink>
<div>
			{{test}}  {{isConnected}} Application des commandes

</div>

				<button v-if="isConnected" @onclick="logout()">LOGOUT</button>
				<button v-else href="/login">LOGIN</button>

	</div>
</template>

<script script="ts" setup>

const isConnected = useState('isConnected');
const test  = useState( 'test' );
console.log( isConnected );

</script>

then just create a default layout pu menu into and

Describe the bug

When we click on button , on the menu in the vif part nothing happen, if we delete the watch part, it will work

Additional context

No response

Logs

no error only my log write the object

isConnected?                                                                                                                                                                    12:47:30
ObjectRefImpl {                                                                                                                                                                 12:47:30
  _object: {},
  _key: '$sisConnected',
  _defaultValue: undefined,
  __v_isRef: true
}

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
leccyrilcommented, Nov 23, 2022

so after talking with vuetify developers don’t use v-text in button and it is OK

0reactions
leccyrilcommented, Nov 23, 2022

ok so finally i could reproduce exactly :

https://stackblitz.com/edit/github-q97ggk

to make it work (as it should work) just rename the file called vuetify.ts in plugins directory

with the file the v-if is not working in any tag

without vuetify plugin it is working

In this exemple clicking on the button make a rest call api, in the result of the watch set var with useState, and update value used in component, i added in a simple bracket to see that is working , but not with the v-if statement

Read more comments on GitHub >

github_iconTop Results From Across the Web

react useState not updating value - Stack Overflow
useEffect doesn't traverse through all props in an object so you have to explicitly put the exact value in the watch list.
Read more >
How To Use and Not Use State - React Training
This doesn't happen with setting state in function components with useState . If you did try to do it like this: const [state,...
Read more >
React Hooks cheat sheet: Best practices with examples
Declaring a state variable is as simple as calling useState with some initial state value, like so: useState(initialStateValue) .
Read more >
Steps to Solve Changes Not Reflecting When useState Set ...
When the “useState” set method is not reflecting a change immediately, it may be due to the current closure of the state variable....
Read more >
4 Examples of the useState Hook - Dave Ceddia
Or if you're more the video type, watch me build a similar ... just call it with a new value, and the state...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found