Skip to content

Latest commit

 

History

History
35 lines (21 loc) · 227 KB

File metadata and controls

35 lines (21 loc) · 227 KB

useLocalStorage medium #Composable Function

By webfansplz @webfansplz

Take the Challenge    Take the Challenge(Passed unit tests)    简体中文

We often need to use the localStorage API. A composable function will help us use it better. Lets go. 👇:

<script setup lang='ts'>

import { ref } from "vue"

/**
 * Implement the composable function
 Make sure the function works correctly
*/
function useLocalStorage(key: string, initialValue: any) {
  const value = ref(initialValue)

  return value
}

const counter = useLocalStorage("counter", 0)

// We can get the localStorage by triggering the getter:
console.log(counter.value)

// And we also can set the localStorage by triggering the setter:

counter.value = 1

</script>

Back Share your Solutions Check out Solutions