Skip to content

Logic within dynamic styles #293

Answered by nmn
edamascus asked this question in Q&A
Jan 1, 2024 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

While some logic is allowed within dynamic styles and it's an issue of our ESLint rule not being smart enough, you should never use dynamic styles for such cases. Do this instead:

const styles = stylex.create({
  base: {
    display: {
      default: 'none',
      [md]: 'flex',
    },
  },
  visible: {
    display: 'flex',
  }
});

// ... within the compoenent
const [visible, setVisible] = useState<boolean>(false);

<div {...stylex.props(styles.base, visible && styles.visible)}>
// ...
</div>

Replies: 1 comment 5 replies

Comment options

nmn
Jan 2, 2024
Collaborator

You must be logged in to vote
5 replies
@nmn
Comment options

nmn Jan 2, 2024
Collaborator

@edamascus
Comment options

@nmn
Comment options

nmn Jan 2, 2024
Collaborator

@edamascus
Comment options

@nmn
Comment options

nmn Jan 3, 2024
Collaborator

Answer selected by nmn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants