#Pass class from parent to child

5 messages · Page 1 of 1 (latest)

hollow knoll
#

I have a component that i use it in other components and sometimes i need to pass a class to it and the css for that class i add it in inside the parent component. In react with css modules i could pass the styles from parent to the child without any issues and without having to make them global but i don't seem to find a way to make this work with astro. Is there a way i could make this work?

peak rose
#

You have to create a class prop and add it to an element in your component, you could also pass it as a prop to a child component

hollow knoll
peak rose
#

You can do something like: ```jsx

const { class: _class } = Astro.props;

<div class:list={[
_class,
"card"
]}></div>

hollow knoll
#

Works, thank you. I was doing the same but without renaming the class prop into my component and when adding it to class:list i was getting an error