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

Code runs twice after adding useColorModeValue hook #5046

Closed
3 tasks
justnoobbot opened this issue Nov 9, 2021 · 12 comments
Closed
3 tasks

Code runs twice after adding useColorModeValue hook #5046

justnoobbot opened this issue Nov 9, 2021 · 12 comments
Labels
stale Type: Bug 🐛 Something isn't working

Comments

@justnoobbot
Copy link

justnoobbot commented Nov 9, 2021

Description

When I run the app i expect console.log("log") to run once, but it got executed twice

Link to Reproduction

https://codesandbox.io/s/chakra-ui-usecolormodevalue-j5y1j?file=/src/App.js

Steps to reproduce

  1. Visit sandbox
  2. Click console
  3. See console.log("log"); got executed twice

When I remove const bg = useColorModeValue("white", "gray"); it runs once as expected
(I need that in my project)

Chakra UI Version

1.6.12

Browser

No response

Operating System

  • macOS
  • Windows
  • Linux

Additional Information

No response

@justnoobbot justnoobbot added needs triage Issues and pull requests that need triage attention Type: Bug 🐛 Something isn't working labels Nov 9, 2021
@anubra266
Copy link
Collaborator

Hi @justnoobbot this is not a bug, it's working as designed by React. useColorModeValue uses the useColorMode hook which consumes ColorModeContextwith the ReactuseContexthook. TheuseContext` hook is what causes the rerender. Get more explanation here

@primos63
Copy link
Contributor

@anubra266 I believe this is the color mode issue that was just fixed by #4989. If the system color mode is dark then the app is rendered with the initialColor mode, light. It then re-renders with dark mode. Using the CSB with the fix, the example that was provided only renders once for light mode.

@anubra266
Copy link
Collaborator

A new version has been released @justnoobbot which includes the fix

@justnoobbot
Copy link
Author

@anubra266 I just updated
check this out: https://codesandbox.io/s/chakra-ui-usecolormodevalue-j5y1j?file=/src/App.js
still re-renders with dark mode

@anubra266
Copy link
Collaborator

@anubra266 I just updated check this out: https://codesandbox.io/s/chakra-ui-usecolormodevalue-j5y1j?file=/src/App.js still re-renders with dark mode

cc @primos63

@anubra266 anubra266 reopened this Nov 13, 2021
@anubra266
Copy link
Collaborator

anubra266 commented Nov 19, 2021

Thank you @anubra266 & @primos63 , is there any workaround for this ?

I started using a react video player (Plyr) and that re rendering is breaking my code

const Component = () => {

  const bg = useColorModeValue("white", "gray");

  const videoSrc = { sources: chapter.source };

  return (  

   // ... 

   <Plyr source={videoSrc} /> // No output

   // ...

  );

}

In the mean time you can use the _dark pseudo prop for mode styling instead of the useColorModeValue hook @justnoobbot

@primos63
Copy link
Contributor

@anubra266 I just updated check this out: https://codesandbox.io/s/chakra-ui-usecolormodevalue-j5y1j?file=/src/App.js still re-renders with dark mode

cc @primos63

My apologies. I missed the notification on this. I looked at the CSB, what am I suppose to observe? I ran the app with and without the commented out code. Each time the log, modified to console.log('log', colorMode), show a single message with the color mode. The text that says "There's nothing here!" displays appropriately regardless of the color mode.

Uncommenting the code, useColorModeValue, and changing the log message to console.log('log', bg) also produced a single log message with the correct color mode value.

@anubra266
Copy link
Collaborator

@anubra266 I just updated check this out: https://codesandbox.io/s/chakra-ui-usecolormodevalue-j5y1j?file=/src/App.js still re-renders with dark mode

cc @primos63

My apologies. I missed the notification on this. I looked at the CSB, what am I suppose to observe? I ran the app with and without the commented out code. Each time the log, modified to console.log('log', colorMode), show a single message with the color mode. The text that says "There's nothing here!" displays appropriately regardless of the color mode.

Uncommenting the code, useColorModeValue, and changing the log message to console.log('log', bg) also produced a single log message with the correct color mode value.

@primos63 try it in dark mode

@primos63
Copy link
Contributor

The text that says "There's nothing here!" displays appropriately regardless of the color mode.

I did. In light mode the text is black and bg white. In dark mode the bg is dark gray and text is white. What am I suppose to be seeing?

@anubra266
Copy link
Collaborator

The text that says "There's nothing here!" displays appropriately regardless of the color mode.

I did. In light mode the text is black and bg white. In dark mode the bg is dark gray and text is white. What am I suppose to be seeing?

@primos63 Check the console in dark mode, the component is rendered twice.

@primos63
Copy link
Contributor

@anubra266 Thanks. I just finished triaging the issue. This is the white flash/double render issue caused by the ColorModeManager. I've tested a local fix with a React app and will follow up with one using Next. Once that's complete I'll submit a PR.

@primos63 primos63 self-assigned this Nov 25, 2021
@primos63 primos63 added Status: Work in Progress 🛠 Someone is working on this and removed needs triage Issues and pull requests that need triage attention labels Nov 25, 2021
@primos63 primos63 removed their assignment Feb 2, 2022
@primos63 primos63 removed the Status: Work in Progress 🛠 Someone is working on this label Feb 2, 2022
@stale
Copy link

stale bot commented Apr 16, 2022

Hi!
This issue has been automatically marked as stale because lack of recent activity. It will be closed if no further activity occurs within 5 days. Thank you for your contributions.

@stale stale bot added the stale label Apr 16, 2022
@stale stale bot closed this as completed Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Type: Bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants