Warning: Prop `src` did not match. Server. And images aren't updated
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top 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 >
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 Free
Top 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

Generally I’d just recommend not randomly shuffling stuff in the UI, but if you really want to you can use
useEffect/setStateThe code gets loaded both server-side and client-side and executes in both cases. That’s the reason stuff gets shuffled twice.