1

Since May 2018, we can write CSS objects in styled components.

Is it possible though to have conditionals in the objects (as we can with "normal" styled components). I cannot get the following conditional to work:

const StyledButton = styled.button({
  backgroundColor: colors.defaultBlue,
  borderRadius: '20px',
  color: props => (props.hover === true ? '#fff' : '#000'),
  border: 'none'
})
2
  • Did you try props => (this.props.hover ? '#fff' : '#000'). Or evaluate outside the object and pass the result in? Commented Sep 25, 2018 at 14:01
  • Presumably you'd need to define this as a function, which takes props and returns styled.button? Commented Sep 25, 2018 at 14:02

1 Answer 1

1

try this

const StyledButton = styled.button((props) => ({
  backgroundColor: colors.defaultBlue,
  borderRadius: '20px',
  color: props.hover === true ? '#fff' : '#000',
  border: 'none'
}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.