Composables: how to share and not to share state between multiple instances? #9612
Unanswered
goodpixels
asked this question in
Help/Questions
Replies: 2 comments 8 replies
-
export function useState<T>(value: T) {
const state:Ref<T> = ref(value)
const setState = (value: T) => {
state.value = value
}
return [state, setState]
} |
Beta Was this translation helpful? Give feedback.
5 replies
-
Share state: const state = ref(false)
export function useToggle() {
const toggle = () => state.value = !state.value
return {
state: readonly(state)
toggle,
}
} no shared state: export function useToggle() {
const state = ref(false)
const toggle = () => state.value = !state.value
return {
state: readonly(state)
toggle,
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've created a composable which does some transformations to passed values and returns the result.
All was working as expected, until I had to render multiple instances of a component which is using this composable internally and the result is that each instance (list items) which is using this composable returns the same values (those set by the first instance). How can I avoid this?
I would like to ensure that the composable does not share its state between instances.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions