admin管理员组

文章数量:1418136

I have been looking at this piece of code for over 2 days now, and i have not been able to locate my Hydration error. It is driving me crazy. Could some one maybe take a look at it for me? Are there any tips and tricks to spot these kind of errors more quickly, would love to know!

I'am using nextjs and using axios for the get resquest

These are the errors: Error: Hydration failed because the initial UI does not match what was rendered on the server.

Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

react-dom.development.js?ac89:19849 Uncaught Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.


  export async function getStaticProps() {
    try {
      const res = await axios.get('.json')
      const events = res.data;
      return {
        props: {
          events: events.slice(0, 10)
        }
      } 
    } catch (error) {
      console.log(error)
    }
  }
  


function EventsCards({events}) {

  return (
    <div>
      <a id="pressable-card max-w-md">
        <div id="featured-event-container" className="bg-black rounded-md bg-opacity-20 bg-blur-sm max-w-xs shadow-lg">
          <div id="event-banner">
            <img className="max-w-lg w-full h-full" src={events.media[0].url }/>
          </div>
          <div className="text-white pl-2">
            <h1 className="text-lg font-medium text-transparent bg-clip-text bg-gradient-to-r from-purple-400 to-pink-600">{events.title}</h1>
            <a className="text-sm uppercase">{events.title}</a>
            <a className="text-xs text-">Friday 20 Jan | 13:00 - 02:00</a>
          </div>
          <div className="py-2 px-2">
            <p className="text-slate-200 font-normal border-[1px] py-[2px] px-[4px] rounded-lg border-slate-400 w-8 text-[8px]">Techno</p>
          </div>
        </div>
      </a>
    </div>
  )
}
function Events({events}) {

  return (
      <div className="bg-gradient-to-t from-gray-500 to-gray-900 h-full bg-blur-sm pt-2">
        <div className="max-w-6xl mx-auto">
          <div className="px-8 ">
            <div className="flex">
              <h1 className="text-white font-regular opacity-100 tracking-wider sm:text-xl md:text-2xl">Featured events in Amsterdam</h1>
              <div className="pl-2 my-auto">
                <img className="rounded-full w-8 h-8 md:w-6 md:h-6  border-gray-400" src=".gif"></img>
              </div>
            </div>
            <ul className="grid grid-cols-1 md:grid-cols-2 pt-4 md:w-full">
              
              <div id="featured-wrapper" className="bg-black rounded-md bg-opacity-20 bg-blur-sm max-w-xs shadow-lg">
                <a id="pressable-card max-w-md">
                  <div id="featured-event-container">
                    <div id="event-banner">
                      <img className="max-w-lg max-h-lg w-full h-full" src='(1).jpg?proxy=true'></img>
                    </div>
                    <div className="text-white pl-2">
                      <h1 className="text-lg font-medium text-transparent bg-clip-text bg-gradient-to-r from-purple-400 to-pink-600">El Row Town 2022 - Holland</h1>
                      <a className="text-sm uppercase">{events.title}</a>
                      <a className="text-xs text-">Friday 1 Jan | 11:00 - 04:00</a>
                    </div>
                    <div className="py-2 px-2">
                      <a className="text-slate-200 font-normal border-[1px] py-[2px] px-[4px] rounded-lg border-slate-400 w-8 text-[8px]">Techno</a>
                    </div>
                  </div>
                </a>
              </div>
              <div className="text-red-400"><h1>test</h1></div>
            </ul>
          </div>
          {/* Amsterdam Events */}
          <div className="flex justify-center py-8">
            <button className="text-[8px] uppercase font-medium rounded-md py-[8px] px-2 bg-white">see events in Amsterdam</button>
          </div>
          {/* All Events */}
          <div className="mx-auto max-w-6xl w-full">
            <h1 className="px-8 text-white font-regular tracking-wider text-xl md:text-2xl">Amsterdam</h1>
          </div>
          <div className="max-auto max-w-6xl">
            <div className="grid grid-cols-1 md:grid-cols-3 pt-4 md:w-full w-full px-8 gap-4">
              {events.map((event) => (
                <EventsCards key={event.id} events={event} />
              ))}
            </div>
          </div>

        </div>
      </div>

  )
}
export default Events;```

I have been looking at this piece of code for over 2 days now, and i have not been able to locate my Hydration error. It is driving me crazy. Could some one maybe take a look at it for me? Are there any tips and tricks to spot these kind of errors more quickly, would love to know!

I'am using nextjs and using axios for the get resquest

These are the errors: Error: Hydration failed because the initial UI does not match what was rendered on the server.

Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

react-dom.development.js?ac89:19849 Uncaught Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.


  export async function getStaticProps() {
    try {
      const res = await axios.get('https://open.data.amsterdam.nl/Festivals.json')
      const events = res.data;
      return {
        props: {
          events: events.slice(0, 10)
        }
      } 
    } catch (error) {
      console.log(error)
    }
  }
  


function EventsCards({events}) {

  return (
    <div>
      <a id="pressable-card max-w-md">
        <div id="featured-event-container" className="bg-black rounded-md bg-opacity-20 bg-blur-sm max-w-xs shadow-lg">
          <div id="event-banner">
            <img className="max-w-lg w-full h-full" src={events.media[0].url }/>
          </div>
          <div className="text-white pl-2">
            <h1 className="text-lg font-medium text-transparent bg-clip-text bg-gradient-to-r from-purple-400 to-pink-600">{events.title}</h1>
            <a className="text-sm uppercase">{events.title}</a>
            <a className="text-xs text-">Friday 20 Jan | 13:00 - 02:00</a>
          </div>
          <div className="py-2 px-2">
            <p className="text-slate-200 font-normal border-[1px] py-[2px] px-[4px] rounded-lg border-slate-400 w-8 text-[8px]">Techno</p>
          </div>
        </div>
      </a>
    </div>
  )
}
function Events({events}) {

  return (
      <div className="bg-gradient-to-t from-gray-500 to-gray-900 h-full bg-blur-sm pt-2">
        <div className="max-w-6xl mx-auto">
          <div className="px-8 ">
            <div className="flex">
              <h1 className="text-white font-regular opacity-100 tracking-wider sm:text-xl md:text-2xl">Featured events in Amsterdam</h1>
              <div className="pl-2 my-auto">
                <img className="rounded-full w-8 h-8 md:w-6 md:h-6  border-gray-400" src="https://www.fotw.info/images/n/nl.gif"></img>
              </div>
            </div>
            <ul className="grid grid-cols-1 md:grid-cols-2 pt-4 md:w-full">
              
              <div id="featured-wrapper" className="bg-black rounded-md bg-opacity-20 bg-blur-sm max-w-xs shadow-lg">
                <a id="pressable-card max-w-md">
                  <div id="featured-event-container">
                    <div id="event-banner">
                      <img className="max-w-lg max-h-lg w-full h-full" src='https://d1as2iufift1z3.cloudfront/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBaWpqIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--696c8f363a91d0501e8ae333fc9d42e5fd9c225f/ERT_HOLLAND_SIGNUP_banner%20(1).jpg?proxy=true'></img>
                    </div>
                    <div className="text-white pl-2">
                      <h1 className="text-lg font-medium text-transparent bg-clip-text bg-gradient-to-r from-purple-400 to-pink-600">El Row Town 2022 - Holland</h1>
                      <a className="text-sm uppercase">{events.title}</a>
                      <a className="text-xs text-">Friday 1 Jan | 11:00 - 04:00</a>
                    </div>
                    <div className="py-2 px-2">
                      <a className="text-slate-200 font-normal border-[1px] py-[2px] px-[4px] rounded-lg border-slate-400 w-8 text-[8px]">Techno</a>
                    </div>
                  </div>
                </a>
              </div>
              <div className="text-red-400"><h1>test</h1></div>
            </ul>
          </div>
          {/* Amsterdam Events */}
          <div className="flex justify-center py-8">
            <button className="text-[8px] uppercase font-medium rounded-md py-[8px] px-2 bg-white">see events in Amsterdam</button>
          </div>
          {/* All Events */}
          <div className="mx-auto max-w-6xl w-full">
            <h1 className="px-8 text-white font-regular tracking-wider text-xl md:text-2xl">Amsterdam</h1>
          </div>
          <div className="max-auto max-w-6xl">
            <div className="grid grid-cols-1 md:grid-cols-3 pt-4 md:w-full w-full px-8 gap-4">
              {events.map((event) => (
                <EventsCards key={event.id} events={event} />
              ))}
            </div>
          </div>

        </div>
      </div>

  )
}
export default Events;```
Share Improve this question edited Nov 7, 2022 at 22:54 DeckChain asked Nov 7, 2022 at 21:04 DeckChainDeckChain 852 silver badges12 bronze badges 5
  • I had a similar problem and it turned out to be a browser extension (specifically in my case Grammarly). Make sure to turn off your extensions that may add data properties into the page, thereby making the page different from what was rendered on the server. – jme11 Commented Nov 7, 2022 at 22:18
  • 1 if you check the console you'll see some validateDOMNesting errors about <a> cannot be descendant of <a> – mocherfaoui Commented Nov 7, 2022 at 22:19
  • 1 I was just going to say that the other thing to check is invalid markup. @mocherfaoui nailed it. – jme11 Commented Nov 7, 2022 at 22:22
  • @mocherfaoui The only thing i get back is the following: Warning: data for page "/" is 270 kB which exceeds the threshold of 128 kB, this amount of data can reduce performance. See more info here: nextjs/docs/messages/large-page-data – DeckChain Commented Nov 7, 2022 at 22:34
  • @jme11 I have added the new error, it does not make me any wiser :3. Could the error be only in the file that i am reloading in the browser or could it be anywhere in my project? – DeckChain Commented Nov 7, 2022 at 22:55
Add a ment  | 

2 Answers 2

Reset to default 2

There are a couple of issues with your code that are causing issues:

  1. Invalid markup (anchor tags inside other anchor tags, ul tags without li children).
  2. You are passing the prop events to your <EventCard /> ponent (it should have a name that is different from the events array retrieved server-side)
  3. You are using a event.id for your key prop when none exists (it should be event.trcid).

Here's a basic working version of your ponents:

export default function Events({ events }) {
  return (
    <>
      {events.map((event) => (
        <EventsCards key={event.trcid} event={event} />
      ))}
    </>
  );
}

function EventsCards({ event }) {
  return <div>{event.title}</div>;
}

export async function getStaticProps() {
  try {
    const res = await axios.get(
      'https://open.data.amsterdam.nl/Festivals.json'
    );
    const events = res.data;
    return {
      props: {
        events,
      },
    };
  } catch (error) {
    console.log(error);
  }
}

More often, it's best to start by getting the data on the page and then add in your additional content and styles.

It happens because you make some changes on the server side and save them before the Fast-Refresh pletes hence causing a hydration error. You can manually reload the page after pleting saving the changes or wait for Fast-Refresh to plete and then save your codes.

本文标签: javascriptNot able to locate Hydration Error Initial UI does not match what was renderedStack Overflow