【问题标题】:Next JS Returned Element Does Not Apply CSS下一个 JS 返回的元素不应用 CSS
【发布时间】:2022-02-06 01:37:57
【问题描述】:

如果我创建一个函数来返回一个与 className 关联的 dom 元素,则本地样式不会被应用,例如;

export default function thing(){
    const elem = function(){
        return (<div className="myCss">Hello</div>)
    }
    
    return(
        <>
            { elem() }
            <style jsx>{` .myCss{ color:#f00 } `}</style>
        </>

    )    
}

我的期望是 elem() 将返回一个元素,该元素将继承与 .myCss 类关联的样式。但它没有:/

【问题讨论】:

标签: css reactjs next.js styled-jsx


【解决方案1】:

default styled-jsx 转换样式名,因此您的示例生成动态样式,例如:.mycss-123 - 如果您希望 .myCss 保持不变,请添加 global - 此外,您的模板字符串包含空格。

这行得通。

<style jsx global>{`.myCss{ color:red }`}</style>

【讨论】:

  • 这是我发现的正确答案。不幸的是,这个标签的热重载很奇怪:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多