Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How do I load Next.js App after promise is resolved?

How do I achieve the code below using Next.js.

I believe there is an issue with Next.js not being able to access the window object unless you're inside a useEffect(() => {}) hook.

Changing to regular React, this code worked fine.

What am I missing in the Next.js ecosystem that doesn't allow this type of delayed render to work?

Thank you.

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { initializeContract } from "./utils/near";

window.nearInitPromise = initializeContract()
  .then(() => {
    ReactDOM.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
      document.getElementById("root")
    );
  })
  .catch(console.error);

reportWebVitals();

Update: Here is my attempt with Next.js

import { useEffect } from "react";
import { initializeContract } from "../utils/near";

function MyApp({ Component, pageProps }) {
  useEffect(() => {
    window.nearInitPromise = initializeContract().then(() => renderApp());
  }, []);

  const renderApp = () => {
    return <Component {...pageProps} />;
  };
}

export default MyApp;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

That's because window object is not available on server-side (nodejs), when you use it inside useHook it's executed client-side. (when the component is mounted).

So if you run your code inside an useEfect this ensures that your code only runs on the client-side.

Window is not defined in Next.js React app

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!