Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
next.js wont let me return in a promise

For background on the problem, my code was working fine in run dev with if-else statements however in run build the if-else didn't work so now I'm trying new ways of conditionally rendering a page. This one is the most promising however the return statement inside doesn't work. Is there a fix? If I replace it with a console.log it returns the text to the console, so it has something to do with the promise.

import { useAddress } from "@thirdweb-dev/react";
import Head from 'next/head';
import Link from 'next/link';
import Username from '../components/Username';
import React from "react";



const Home = () => {

    let p = new Promise((resolve, reject) => {
        let address = useAddress();
        if (address) {
            resolve(address)
        } else {
            console.log('no addy')
            reject()
        }
    })
    
    p.then((address) => {
        return (
            <>
                <Head>
                    <title>DRUMM3R</title>
                    <link rel="icon" href="/drum.svg" />
                </Head>
                <Username address={address} />
            </>
        );
    }).catch(() => {
        return (
            <>
                <Head>
                    <title>DRUMM3R</title>
                    <link rel="icon" href="/drum.svg" />                
                </Head>
                <Link href="/">
                    <a className="absolute pt-1 text-xl font-semibold transform -translate-x-1/2 top-1/2 left-1/2">click here to log in</a>
                </Link>
            </>
        );
    })        
}

export default Home;

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issues isn't with Next, its that React expects a valid JSX element to be returned. Your function doesn't return anything, implicitly returning undefined and that will break.

So what you need to do is either have some type of effect (if you're doing something async) that will setState and render the appropriate element (loading indicator, error message, or data, etc) or you need to have that data already there for the component to render.

import { useAddress } from "@thirdweb-dev/react";
import Head from "next/head";
import Link from "next/link";
import Username from "../components/Username";
import React from "react";

const Home = () => {
  const address = useAddress();

  return address ? (
<>
  <Head>
    <title>DRUMM3R</title>
    <link rel="icon" href="/drum.svg" />
  </Head>
  <Username address={address} />
</>
  ) : (
<>
  <Head>
    <title>DRUMM3R</title>
    <link rel="icon" href="/drum.svg" />
  </Head>
  <Link href="/">
    <a className="absolute pt-1 text-xl font-semibold transform -translate-x-1/2 top-1/2 left-1/2">
      click here to log in
    </a>
  </Link>
</>
  );
};

You can do something like this and it should work.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda