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.

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

  1. 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;
  1. Ensure both navbar.logo.src and navbar.logo.srcDark are set in the config.

  2. Build + load in browser.

  3. 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:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SamChou19815commented, May 25, 2020

After some investigation, I found the reason.

First, you should not use useTheme(). It’s designed to be used internally by useThemeContext(). You should use useThemeContext() instead. useLogo is fine since it calls useThemeContext() under the hood.

useThemeContext(), as the name implies, must work under some context. The context is <ThemeProvider>, which is part of Layout. 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

import React from 'react';
import Layout from '@theme/Layout';

function Page() {
  return (
    <Layout title="Foo" description="bar">
      Baz
    </Layout>
  );
}

export default Page;

If you try to useLogo, it might become:

import React from 'react';
import Layout from '@theme/Layout';

function Page() {
  // oops, use the ThemeProvider without being its child.
  const logo = useLogo();

  return (
    <Layout title="Foo" description="bar">
      Baz
    </Layout>
  );
}

export default Page;

A workaround is to move the content of the page to another component:

import React from 'react';
import Layout from '@theme/Layout';

function Content() {
  const logo = useLogo();
  return logo;
}

function Page() {
  return (
    <Layout title="Foo" description="bar">
      <Content />
    </Layout>
  );
}

export default Page;
1reaction
kimburgesscommented, Apr 23, 2020

I’m still building up my mental model of this project, but in case it’s of relevance pulling isDarkTheme directly from useTheme does work, but only on initial page load.

Read more comments on GitHub >

github_iconTop 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 >

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