Skip to content

How to define dynamic variable #206

Closed Answered by nmn
nucleartux asked this question in Q&A
Discussion options

You must be logged in to vote

Step 1

Define default values for variables using defineVars. These default values cannot be dynamic. These are the default values.

Step 2

Use stylex.create to set values for variables dynamically:

const styles = stylex.create({
  dynamicTheme: (themeFromAPI) => ({
    [colors.accent]: themeFromAPI.accent,
  }),
});

// Somewhere near the top of the app:
<html {...stylex.props(styles.dynamicTheme(themeFromAPI))}>
  {children}
</html>

NOTE:

  • This solution works and the API is stable.
  • There will further performance optimisations for this in the near future.
  • We might also add support for functions to createTheme if this is a common use-case

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
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