【发布时间】:2021-05-20 13:32:08
【问题描述】:
使用styled-components时如何访问global.js中的ThemeProviderprops?
例如在 theme.js 中我有 ${props => props.theme.fonts.fontSize} 调用默认字体大小 16px
const theme = {
fonts: {
fontSize : '16px',
}
}
export default theme
这是在/layouts/index.js 中提供的
import React from 'react'
import { ThemeProvider } from 'styled-components'
import '../style/global';
import theme from '../style/theme'
class Template extends React.Component {
render() {
const { children } = this.props
return (
<ThemeProvider theme={theme}>
...
{children()}
...
</ThemeProvider>
)
}
}
export default Template
从这里我可以访问每个组件或子页面中的${props => props.theme.fonts.fontSize}。
但是,当 global 在技术上高于 theme.js 时,我如何以同样的方式传递给 global.js?这样我就可以创建一个全局样式为
injectGlobal`
html {
font-size: (${props => props.theme.fonts.fontSize} / 16px) * 1em;
}
`
【问题讨论】:
标签: javascript reactjs styled-components