K
K
Karin Park2020-10-07 12:06:00
css
Karin Park, 2020-10-07 12:06:00

How to register classes in css modules depending on the condition (React)?

Hello.
I have a react project. The appointments component has a parent block without styles.
I want to use this component in two different places (components) with different styles depending on where it is called. What is the best way to implement this? Or tell me where to read.
for a better explanation:

The appointments component:
Condition: if this is the LastAppointments component, then the ROW class is applied to the block, if it is the AllAppointments component, then the COLUMN class.

<div className={Row или Column}> какой то код </div>


the LastAppointments component:
calling the appointments component with the row class.

AllAppointments component:
calling appointments component with class column

Answer the question

In order to leave comments, you need to log in

2 answer(s)
8
8XL, 2020-10-07
@Karin_Park

You can do without extra states. That is, you pass its name to the props of the component (well, or any other identifier)
<LastAppointments name={'LastAppointments'} />

<div className={ props.name == 'LastAppointments' ? 'row' : 'column' }> какой то код </div>

In the diva itself, you check what name came in the props.

K
Kirill Makarov, 2020-10-07
@kirbi1996

create
[state, setState] = useState(false) change
it as needed

<div className={ state ? "row" : "column"}> какой то код </div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question