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.

useColorMode not reactive when using multiple instances

See original GitHub issue

When I use useColorMode() in different places, different refs are returned, which means reactivity between them doesn’t work, e.g.

// ColorMode toggle component

<template>
  Some color mode toggle
</tempalte>

<script setup lang="ts">
const color = useColorMode()
// manipulate color with toggle...
</script>
// Some other component

<template>
  <div :style="{ 'background-color': bgColor }">
    Some HTML
  </div>
</tempalte>

<script setup lang="ts">
const color = useColorMode()
const bgColor = computed(() => color === 'light' ? '#fff' : '#000')
</script>

Now, when I use the toggle to change the color, the second component will not react…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

0reactions
okxiaoliang4commented, Feb 19, 2022

I think if you want reactive colorMode in multiple instance it would be a little better to export its module (less function call) or try createGlobalState.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug: useColorMode returned undefined colorMode if you use ...
Bug report Describe the bug I'm not sure, this is a bug or just the default behaviour of react hooks, but let me...
Read more >
useColorMode not working for Chakra UI Footer Component
Alternative problem: Maybe you have multiple ChakraProvider 's - One that wraps your whole App, and one that wraps everything except your footer ......
Read more >
Functions | VueUse
useColorMode -reactive color mode (dark / light / customs) with auto data ... onStartTyping-fires when users start typing on non-editable elements.
Read more >
Color Mode - Chakra UI
Color Mode. Chakra UI comes with built-in support for managing color mode in your apps. By default, most of Chakra's components are dark...
Read more >
12 React Hooks Every React Developer Should Know
React hooks are highly reusable parts of functionality. Hooks let us Eencapsulate side effects and compose and reuse logic. For most use cases, ......
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