Warning: Prop `src` did not match. Server. And images aren't updated

See original GitHub issue

Bug report

Describe the bug

Warning: Prop src did not match. Server. And images aren’t updated when shuffling an array before rendering it

To Reproduce

Create an array outside of a component and shuffle it before rendering (When runs on server only)

Expected behavior

Shouldn’t throw client and server mismatch and update the images correctly

reproduce or CodeSandbox

https://codesandbox.io/s/musing-cray-11zoo

import Link from "next/link";

const list = [
  {
    url: "/now.png",
    name: "Now"
  },
  {
    url: "/next.png",
    name: "Next"
  },
  {
    url: "/mdx.png",
    name: "MDX"
  },
  {
    url: "/lambda.jpg",
    name: "lambda"
  },
  {
    url: "/github.png",
    name: "github"
  }
];

const shuffledList = shuffleArray(list);

const index = () => {
  console.log(shuffledList);
  return (
    <div>
      <Link href="/about">
        <a>About</a>
      </Link>
      <div style={{ display: "flex", flexWrap: "wrap" }}>
        {shuffledList.map(item => (
          <div
            key={item.name}
            style={{
              border: "1px solid black",
              margin: "10px",
              textAlign: "center"
            }}
          >
            <img src={item.url} alt={item.name} style={{ width: "200px" }} />
            <p>{item.name}</p>
          </div>
        ))}
      </div>
    </div>
  );
};

export default index;

function shuffleArray(arr) {
  return arr
    .map(a => [Math.random(), a])
    .sort((a, b) => a[0] - b[0])
    .map(a => a[1]);
}

System information

  • Version of Next.js: 9.1.6

Additional context

No

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
timneutkenscommented, Feb 20, 2020

Generally I’d just recommend not randomly shuffling stuff in the UI, but if you really want to you can use useEffect / setState

3reactions
timneutkenscommented, Feb 20, 2020

The code gets loaded both server-side and client-side and executes in both cases. That’s the reason stuff gets shuffled twice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning "Prop `src` did not match" in React - Stack Overflow
I know that I get this error because the code is running on server-side and on client-side with different 'results', but I don't...
Read more >
Tag: Server-side Rendering - somewhat abstract
Console output showing warning that says "Prop 'src' did not match" along Hydration error in React 17.0. This is from React 17, though...
Read more >
React and React Router Server Rendering
In the console, you'll see a warning Text content did not match. Server: "Muthuserver" Client: "Muthu" . Here's what the React docs have...
Read more >
@astrojs/image Astro Documentation
This Astro integration makes it easy to optimize images in your Astro project, with full support for SSG builds and server-side rendering!
Read more >
Frequently Asked Questions - Material UI - MUI
You need to verify that your client and server are running the exactly the same version of MUI. It is possible that a...
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