To change a CSS variable value on button click in React using JavaScript, you can follow these steps:
Define your CSS variable in your CSS file or inside a style tag in your component. Let's assume you have a CSS file called styles.css:
35 chars4 linesImport the CSS file or style in your React component:
79 chars7 linesCreate a state variable in your React component to store the value of the CSS variable:
131 chars8 linesAssign the value of the CSS variable to the state variable:
402 chars18 linesWhenever the button is clicked, call the handleClick function to update the state variable (primaryColor) to the desired value (e.g., "red" in this example).
Use the updated state variable in your CSS to dynamically change the value of the CSS variable (e.g., set the text color to the primaryColor state variable in the h1 element's style prop).
Now, when the button is clicked, the CSS variable value will be updated, and the text color will change accordingly.
Remember to replace "blue" and "red" with actual values you want to use.
Note: If you want to change multiple CSS variables at once, you can create separate state variables for each and update them accordingly.
gistlibby LogSnag