简体   繁体   English

导入css变量到next.js中的tsx文件

[英]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:我正在使用 next.js,假设我有一个 myStyles.css 文件,如下所示:

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

Now I want to use this variable value in the.tsx file.现在我想在 .tsx 文件中使用这个变量值。 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.它可以工作,但看起来不太好,因为您需要等到 DOM 元素加载完毕,所以您必须将它放在 React.useEffect() 中才能使其工作。 So maybe there's a better way?那么也许有更好的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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