最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Next.jsReact warning when generating random values in a component - Stack Overflow

programmeradmin10浏览0评论

I'm building a next.js app that generates some random numbers which generates the warning:

Warning: Text content did not match. Server: "1" Client: "2"

I think I understand why I get this warning (the virtual DOM is kinda-sorta-out-of-sync with what was sent from the server). I'm just wondering if there's a way to let next.js/React know that this is ok in this situation. Or is there a way to only generate the random on the server and let the client use this as a literal?

Or should I just ignore the warning?

I'm building a next.js app that generates some random numbers which generates the warning:

Warning: Text content did not match. Server: "1" Client: "2"

I think I understand why I get this warning (the virtual DOM is kinda-sorta-out-of-sync with what was sent from the server). I'm just wondering if there's a way to let next.js/React know that this is ok in this situation. Or is there a way to only generate the random on the server and let the client use this as a literal?

Or should I just ignore the warning?

Share Improve this question edited Nov 26, 2019 at 19:24 Koen asked Nov 28, 2017 at 19:58 KoenKoen 3,6831 gold badge36 silver badges58 bronze badges 7
  • In which context/method are you creating the random values? – Fabian Schultz Commented Nov 30, 2017 at 9:30
  • @FabianSchultz The ponent constructor. – Koen Commented Nov 30, 2017 at 10:34
  • also have the same problem - have 'generated' value for link (it's intended), but can't find any solution to remove that warning from my console – andrey Commented Mar 1, 2019 at 10:06
  • for nextjs questions, i'd remend going to spectrum.chat and asking there, as a lot of the developers on there use next.js – alex067 Commented Nov 26, 2019 at 19:27
  • 1 @Koen Thanks for the reply. I kinda found a workaround. Not ideal, but no more warning at least. stackoverflow./a/59976790/1526406 – Cully Commented Jan 30, 2020 at 18:22
 |  Show 2 more ments

3 Answers 3

Reset to default 12

Moving JavaScript random variable into React state solved the problem for me.

Here's my problem (simplified) before:

function RandomNumber() {
  const randomNumber = Math.floor(Math.random() * 100);
  return <p>{randomNumber}</p>;
}

After

function RandomNumber() {
  const [randomNumber, setRandomNumber] = useState(undefined);

  useEffect(() => {
    setRandomNumber(Math.floor(Math.random() * 100));
  }, []);

  return <p>{randomNumber}</p>;
}

My code used React Hooks useState and useEffect, but previous React lifecycle methods setState and ponentDidMount should work fine too

What I would suggest is that you wrap the content that you have some random generated content in, inside a ponent.

ponents/NoSsr.js

import dynamic from 'next/dynamic';
import React from 'react';

const NoSsr = ({ children }) => <>{children}</>;

export default dynamic(() => Promise.resolve(NoSsr), {
  ssr: false,
});

And then in your file:

<NoSsr>
    { Math.random() }
</NoSsr>

I would do it with useMemo:

 const randomNumber = useMemo(() => Math.floor(Math.random() * 100), []);
发布评论

评论列表(0)

  1. 暂无评论