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

150
Vistas
Next.js default component value

I am trying to create a global variable that all components are rendered with by default and set that default value but I'm not sure how to do the 2nd part. Here's what I have so far in my _app.tsx:

import { AppProps } from "next/app";
import type { NextComponentType  } from 'next'
import Blue from "../components/blue";
 
type CProps = AppProps & {
   Component: NextComponentType & {model?: string }
};
 
const MyApp = ({
   Component,
   pageProps: { ...pageProps },
}: CProps) => {
   return (
       <>
           {Component.model === 'blue' ? (
               <Blue>
                   <Component {...pageProps} />
               </Blue>
           ) : (
               <Component {...pageProps} />
           )}
       </>
   );
};

But this obviously doesn't give me a default value for model. It just creates that variable with null value for all the components. How do I set the value?

Side question: Is this better done using React Context?

Edit 1: This is how the component sets the model value if it does not want to use the default value:

const ComponentFoo = () => {
   return (
       <>Test</>
   );
};
 
ComponentFoo.model = 'red'
 
export default ComponentFoo;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This sounds like a good candidate for Next.js Layouts. You would have to compose a Layout component, similar to Blue in your example, which accepts a color prop and encapsulates the color rendering logic to the layout file. You can implement a default render path if no color prop is provided.

Then you can use it like so:

// pages/whatever.tsx
import type { ReactElement } from 'react'
import Layout from '../components/layout'

export default function Page() {
  return {
    /** Your content */
  }
}

Page.getLayout = function getLayout(page: ReactElement) {
  return (
    <Layout color="blue">
      {page}
    </Layout>
  )
}

https://nextjs.org/docs/basic-features/layouts#with-typescript

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