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.

use useEffect hooks in <BrowserOnly/> issuse

See original GitHub issue

🐛 Bug Report

Prerequisites

  • [√] I’m using the latest version of Docusaurus.
  • [√] I have tried the npm run clear or yarn clear command.
  • [√] I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.

Description

When I use useEffect in my development BrowserOnly, the console reports the following error

export default function BlogTagsPostPage(props) {
    return (
        <BrowserOnly fallback={null}>
            {() => {
                useEffect(() => {
                    console.log(11);
                }, []);
                return <span></span>;
            }}
        </BrowserOnly>
    );
}

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
slorbercommented, Oct 29, 2021

Use this instead:

export default function BlogTagsPostPage(props) {
  return <BrowserOnly fallback={null}>{() => <Time />}</BrowserOnly>;
}

function Time() {
  return <span>{require("clientOnlyLib").getSomeClientValue()}</span>;
}
2reactions
Josh-Cenacommented, Oct 28, 2021

这是因为你的代码在require的时候就已经被解析了,所以你应该把整个引入 dependency 的代码放在useEffect里面。另外,useIsBrowser 这个钩子你也可能想了解一下

Read more comments on GitHub >

github_iconTop Results From Across the Web

React.useEffect Hook – Common Problems and How to Fix ...
The first and probably most obvious option is to remove the dependency from the useEffect dependency array, ignore the ESLint rule, and move...
Read more >
Using the Effect Hook - React
We pass a function to the useEffect Hook. This function we pass is our effect. Inside our effect, we set the document title...
Read more >
React hook using useEffect updates on web browser but not ...
The problem, which only happens on mobile, is that when hash updates, the surrounding 'squares' get updated images, but the current 'square' ...
Read more >
How the useEffect Hook Works (with Examples) - Dave Ceddia
The useEffect hook is the Swiss Army knife of all the hooks. It's the solution to many problems: how to fetch data when...
Read more >
The last guide to the useEffect Hook you'll ever need
With useEffect , you invoke side effects from within functional components, which is an important concept to understand in the React Hooks era....
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