在全局样式组件中使用 ThemeProvider 道具
Using ThemeProvider props in Global Styled-Components
如何在使用 styled-components 时访问 global.js
中的 ThemeProvider
props
?
例如,在 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}
。
但是,当全局在技术上高于 theme.js
时,如何以相同的方式传递给 global.js
?这样我就可以创建一个全局样式 as
injectGlobal`
html {
font-size: (${props => props.theme.fonts.fontSize} / 16px) * 1em;
}
`
解决这个问题的最简单方法是创建一个顶级组件,像这样注入您想要的样式:
import { Children } from 'react';
import { withTheme, injectGlobal } from 'styled-components';
const GlobalComponent = ({ theme, children }) => {
injectGlobal`
font-size: ${theme.fonts.fontSize}
}
`;
return Children.only(children);
};
export default withTheme(Global);
这将确保将此组件作为父组件的所有组件都将具有所需的全局样式。希望这对您有所帮助
晚了,但现在我们实际上可以创建一个全局组件并将其作为 ThemeProvider
的子项传递。它将允许您访问当前 theme
.
的所有道具
应用字体系列的示例:
你的Global.js / Global.ts
import { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
html,
body {
padding: 0;
margin: 0;
font-family: ${(props) => props.theme.font.family}
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
`;
export default GlobalStyle;
你的主要组成部分app.tsx / app.jsx
import theme...
import { ThemeProvider } ...
imort GlobalStyle from '../path-to-global-file';
const App ...
.
.
return(
<>
<ThemeProvider theme={theme}>
<GlobalStyle />
{ /* Root component */ }
<Component/>
</ThemeProvider>
</>
);
现在可以轻松使用道具了
如何在使用 styled-components 时访问 global.js
中的 ThemeProvider
props
?
例如,在 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}
。
但是,当全局在技术上高于 theme.js
时,如何以相同的方式传递给 global.js
?这样我就可以创建一个全局样式 as
injectGlobal`
html {
font-size: (${props => props.theme.fonts.fontSize} / 16px) * 1em;
}
`
解决这个问题的最简单方法是创建一个顶级组件,像这样注入您想要的样式:
import { Children } from 'react';
import { withTheme, injectGlobal } from 'styled-components';
const GlobalComponent = ({ theme, children }) => {
injectGlobal`
font-size: ${theme.fonts.fontSize}
}
`;
return Children.only(children);
};
export default withTheme(Global);
这将确保将此组件作为父组件的所有组件都将具有所需的全局样式。希望这对您有所帮助
晚了,但现在我们实际上可以创建一个全局组件并将其作为 ThemeProvider
的子项传递。它将允许您访问当前 theme
.
应用字体系列的示例:
你的Global.js / Global.ts
import { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
html,
body {
padding: 0;
margin: 0;
font-family: ${(props) => props.theme.font.family}
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
`;
export default GlobalStyle;
你的主要组成部分app.tsx / app.jsx
import theme...
import { ThemeProvider } ...
imort GlobalStyle from '../path-to-global-file';
const App ...
.
.
return(
<>
<ThemeProvider theme={theme}>
<GlobalStyle />
{ /* Root component */ }
<Component/>
</ThemeProvider>
</>
);
现在可以轻松使用道具了