useLogo hook does not update on theme change
See original GitHub issue🐛 Bug Report
When using the useLogo hook provided by theme-classic in a custom page, the associated attributes do not update following a chance between dark/light themes.
Have you read the Contributing Guidelines on issues?
👍
To Reproduce
- Create a custom page with the following minimal content:
import React from 'react';
import Layout from '@theme/Layout';
import useLogo from '@theme/hooks/useLogo';
function LogoTest() {
const {logoImageUrl} = useLogo();
return (
<Layout title="LogoTest">
<img src={logoImageUrl} />
</Layout>
);
}
export default LogoTest;
-
Ensure both
navbar.logo.src
andnavbar.logo.srcDark
are set in the config. -
Build + load in browser.
-
Toggle theme light/dark state.
Expected behavior
src
attribute should change between src
and srcDark
values.
Actual Behavior
src
remains on original value.
Your Environment
- Docusaurus version used: v2.0.0-alpha.50
- Environment name and version (e.g. Chrome 78.0.3904.108, Node.js 10.17.0): FF 72.0.2
- Operating system and version (desktop or mobile): NixOS (linux)
Reproducible Demo
See above.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
state hooks is not updating state on change - Stack Overflow
I have tried many times by changing the method of setting state but it is still showing that error. App.js function BlogDetail() {...
Read more >How do I set the theme logo? - Drupal Answers
After Drupal 8.6.x. In your THEME.info.yml add a line like logo: logo.png . You need to clear cache for Drupal to detect this...
Read more >Create a React hook to add dark theme to your app
Update the theme when browser's theme changes. To be notified of the browser's theme change, we can use our media query list returned...
Read more >Themes - Looker Studio Help - Google Support
Applying a theme updates the style settings of your charts, tables, background color, and text, helping to ensure your reports are visually attractive...
Read more >How to Add Support for Dark and Light Themes in React ...
In this tutorial, let's create a demo app that is going to change ... We are going to use React Hooks to set...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After some investigation, I found the reason.
First, you should not use
useTheme()
. It’s designed to be used internally byuseThemeContext()
. You should useuseThemeContext()
instead.useLogo
is fine since it callsuseThemeContext()
under the hood.useThemeContext()
, as the name implies, must work under some context. The context is<ThemeProvider>
, which is part ofLayout
. See https://github.com/facebook/docusaurus/blob/d391a2bcdbe7d5096d08be88d8bf5467e70264a8/packages/docusaurus-theme-classic/src/theme/Layout/index.js#L52.In a page, you might have something like
If you try to
useLogo
, it might become:A workaround is to move the content of the page to another component:
I’m still building up my mental model of this project, but in case it’s of relevance pulling
isDarkTheme
directly fromuseTheme
does work, but only on initial page load.