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

18 - useLocalStorage #2863

Open
geek4ctrl opened this issue Dec 28, 2024 · 0 comments
Open

18 - useLocalStorage #2863

geek4ctrl opened this issue Dec 28, 2024 · 0 comments

Comments

@geek4ctrl
Copy link

// your answers
<script setup lang='ts'> import { ref, watchEffect } from 'vue' /** * Implement the composable function * Make sure the function works correctly */ function useLocalStorage(key: string, initialValue: any) { const value = ref(localStorage.getItem(key) != undefined ? localStorage.getItem(key): initialValue); watchEffect(() => { localStorage.setItem(key, value.value) }) return value; } const counter = useLocalStorage("counter", 0) // We can get localStorage by triggering the getter: console.log(counter.value) // And we can also set localStorage by triggering the setter: const update = () => {counter.value++} </script>

Counter: {{ counter }}

Update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant