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

239
Views
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 answers
Answer question

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 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!