简体   繁体   中英

Import css variable to the tsx file in next.js

I'm working with next.js and let's say I have a myStyles.css file like this:

.root {
 --my-custom-variable: 1;
}

Now I want to use this variable value in the.tsx file. How can I do this?

This is what i got so far:

import styles from './myStyles.css';

const result = getComputedStyle(document.getElementsByClassName(styles.root)[0]).getPropertyValue('--my-custom-variable')

It's working but doesn't look very nice, because you will need to wait until the DOM element is loaded, so you'll have to put this inside React.useEffect() to make it work. So maybe there's a better way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM