Styled-Components: Conditional styling with multiple props / states

Nick

I can't seem to find anything about this on Google... Chances are it's there and I'm just not Googling the right thing. I just keep finding what I've already done for one prop.

I currently have styles based on props and states. And they work as expected. But I've found myself in a scenario where I need a style where two props (true/false) are required at the same time for the desired result.

I was wondering if that was possible?

The below works:

${({ prop1 }) => !prop1 &&`
    // Some Styles when !prop1
`};

${({ prop2 }) => prop2 &&`
    // Some Styles when prop2
`};

What I'm trying to do is something like the below:

${({ prop1, prop2 }) => !prop1, prop2 &&`
    // Some Styles when !prop1 && prop2
`};

or

${({ prop1 && prop2 }) => !prop1 && prop2 &&`
    // Some Styles when !prop1 && prop2
`};

But that doesn't seem to want to work. I don't think I'm far off... Maybe I am? Any help would be great!

Thanks a lot.

John Ruddell

you should use the css method to do conditionals

import styled, { css } from 'styled-components'

${({ prop1, prop2 }) => !prop1 && prop2 && css`
    // Some Styles when !prop1 && prop2
`};

notice syntactially you are destructuring the props in your function.

({prop1 && prop2}) == invalid syntax

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Conditional styling with Styled Components

Multiple Props Options for Styled Components

React class components - conditional styling based on props

How to pass props in to MUI styled menu to achieve certain conditional styling?

How to apply the same props to multiple styled components?

Check multiple props with Styled-Components

With Styled Components, why does a conditional style not have access to props?

Styled Components: props for hover

conditional rendering in styled components

Styling based on attr on Styled components

Styling react-router-dom Link using styled-components getting warning when passing props

React.js child component not updating styling derived from props, why? (styled-components)

With styled-components in React JS, what is the more preferred way to add conditional styling?

Styling nested components using styled components

Styled Components rendering Styled tag based on props

Using styled components with props and typescript

Mapping Through Props in Styled Components

Responsive Props In ReactJS Styled Components

how to pass props to styled components

Styled-components extends props

Using conditional statements with styled components

Transition between states using Styled Components

Using props in React with styled components across components

Add styling to child element using Styled Components?

Styling a separate functional component with Styled Components

Sibling component styling with styled-components

Styling textarea in MUI using Styled Components

Contextual styling of ReactJS styled-components

React Native - Styling buttons with styled-components