Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda