Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass in two arguments to a props function in styled components

I have a styled-components in my React component and it currently uses the props feature on styled components.

I need to style using another property but I keep getting an error when I try to pass it in.

This is what I currently have and this works fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

border: 1px solid ${(props) => (props.isError ? "red" : "transparent")};

This is what I am trying to achieve:

border: 1px solid ${(props) => props.isError ? theme.Colors.red : "transparent"};

Is there a way to pass in the theme as an additional prop in the props function?

I keep getting this error **Cannot find name 'theme'.**, even though it is being used in other parts of my styled component.

>Solution :

${({theme, isError}) => ...} is what you’re looking for.

You need to destructure all the props out of the props object – or otherwise you’d use props.theme.Colors.red – as props will contain the theme object the way you’re writing it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading