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

238
Vistas
Conditional css in react/Next js

I am using Next js,I am applying conditional on component which is working,but css style changes between different component.

Code 1:

import React from "react";
import Profile from "../../components/model/Profile";
import { useAuthContext, useAuthUpdateContext } from "../../app/AuthContext"

function profile() {
  const user = useAuthContext()
  const { updateUser } = useAuthUpdateContext()

  return user.isLoggedIn === true && user.user.userType === "Model" ? (
    <div>
      <Profile />
    </div>
  ) : (
    <div className="">
      <h1>You are not authorized to view this page</h1>
      {/* tw-mx-auto tw-mt-auto tw-font-extrabold tw-capitalize tw-text-3xl */}
    </div>
  )
}

export default profile;

Image of code 1 style

import React from "react";
import Profile from "../../components/model/Profile";
import { useAuthContext, useAuthUpdateContext } from "../../app/AuthContext"

function profile() {
  const user = useAuthContext()
  const { updateUser } = useAuthUpdateContext()

  return user.isLoggedIn === true && user.user.userType === "Model" ? (
    <div>
      <Profile />
    </div>
  ) : (
    <div className="tw-mx-auto tw-mt-auto tw-font-extrabold tw-capitalize tw-text-3xl">     <-----// Difference is here from code 1 
      <h1>You are not authorized to view this page</h1>
      {/* tw-mx-auto tw-mt-auto tw-font-extrabold tw-capitalize tw-text-3xl */}
    </div>
  )
}

export default profile;

Image of code 2 style

Components are working as expected but css style not.

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

0

If you are using CSS or SCSS modules, you will first have to import the style sheet in the component where you want to use the styles.

So in your example above, you would use something like:

import React from "react";
import Profile from "../../components/model/Profile";
import { useAuthContext, useAuthUpdateContext } from "../../app/AuthContext"
import styles from "../yourPath/../toStyles/stylesheet.module.css"

Don't forget to use "module.scss" if you are using SCSS in your project.

After that, you can treat the imported styles as object in JS code. So something like this in your code above:

<div className={styles.myClass}>     <-----// Difference is here from code 1 
      <h1>You are not authorized to view this page</h1>
      {/* tw-mx-auto tw-mt-auto tw-font-extrabold tw-capitalize tw-text-3xl */}
    </div>

You can find more info in the official docs -> https://nextjs.org/docs/basic-features/built-in-css-support

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