styled-components 相关问题

styled-components是一个用于样式化React应用程序的JavaScript库。它删除了样式和组件之间的映射,并允许您编写使用JavaScript扩充的实际CSS。

如何在 Next.js 上的活动页面上设置链接组件的样式

我有一个关于在活动页面上设置锚组件样式的问题。 这是我的代码: 从“下一个/链接”导入链接; 从“样式组件”导入样式; const NavStyle = styled.na...

回答 4 投票 0

如何在 React 中将样式组件的 themeProvider 访问到 Portal 中?

我从样式组件设置了主题提供程序,然后创建了一个门户,并且门户中的元素无法访问提供程序 这是我的提供者: 我从样式组件设置了主题提供程序,然后创建了一个门户,并且我的门户中的元素无法访问提供程序 这是我的提供者: <ConfigProvider> <ThemeProvider theme={myTheme}> //<-- provider from styled-components <App /> </ThemeProvider> </ConfigProvider> 这就是我创建门户的方式: export const SetSelector = ()=>{ const OptionsModal = useMemo(() => { return ( <Wrapper $theme={configState.theme}> <Modal $theme={configState.theme}> // more components here, like buttons </Modal> </Wrapper> ); }, [ . . . ]); return ( <ListSelectorWrapper> {displayOptions && createPortal(OptionsModal, document.body)} </ListSelectorWrapper> ); }; } 我的模态组件无法从样式组件访问主题提供程序,我收到此错误:Uncaught TypeError: Cannot read properties of undefined (reading '300'),它来自我的按钮的属性: background: ${props=>props.$variant==="flat"?"transparent":props.$theme==="dark"?props.theme.gray[300]:props.theme.gray[600]}; 另外,我尝试将我的门户包装在提供商中,但不起作用,我遇到了同样的错误 const OptionsModal = useMemo(() => { return ( <ThemeProvider theme={myTheme}> <WrapperOverlay $theme={configState.theme}> <Modal $theme={configState.theme}> // more components here, like buttons </Modal> </WrapperOverlay> </ThemeProvider> ); }, [...]); 如何在我的门户组件中访问主题提供程序值? 我使用样式组件的方式。 创建主题上下文 // ./context/ThemeContext.jsx import { useState, createContext } from 'react' export const ThemeContext = createContext() export const ThemeProvider = ({ children }) => { const [theme, setTheme] = useState('light') return ( <ThemeContext.Provider value={{ theme, setTheme }}> {children} </ThemeContext.Provider> ) } 创建自定义挂钩:“useTheme”。 // ./hooks/useTheme.jsx import { useContext } from "react" import { ThemeContext } from "../context/ThemeContext" export function useTheme() { const { theme, setTheme } = useContext(ThemeContext); const toggleTheme = () => { setTheme(t => t === 'light' ? 'dark' : 'light') } return { theme, toggleTheme } } 创建主题按钮。 // ./components/ThemeButton.jsx import { useTheme } from '../hooks/useTheme'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faLightbulb, faMoon } from '@fortawesome/free-regular-svg-icons'; // create the ButtonWrapper component // retrieve data from the mode property // change color when the darkmode is active const ButtonWrapper = styled.button` color: ${(props) => props.mode === 'dark' && 'white'}; background-color: transparent; border: none; ` const ThemeButton = () => { // use custom hook const { theme, toggleTheme } = useTheme() return ( // add "mode" props to the component // that will have the value "theme" <ButtonWrapper type="button" mode={theme} onClick={() => toggleTheme()} > {theme === 'light' ? ( <FontAwesomeIcon icon={faMoon} /> ) : ( <FontAwesomeIcon icon={faLightbulb} /> )} </ButtonWrapper> ); } export default ThemeButton; 创建 StyledProvider 💬 我使用 Styled-Components 中的 ThemeProvider,就像我将样式变量与 SASS 一起使用一样。 // ./context/StyledContext.jsx import { ThemeProvider } from 'styled-components'; const styled = { colors: { black: "black", white: "white", }, } export const StyledProvider = ({ children }) => { return <ThemeProvider theme={styled}>{children}</ThemeProvider>; } 创建全局样式 // GlobalStyle.jsx import { createGlobalStyle } from "styled-components"; import { useTheme } from "./hooks/useTheme"; // {props => props.mode} to retrieve data from the mode property // {props => props.theme} to retrieve data from StyledProvider const GlobalStyleComponent = createGlobalStyle` body { background-color: ${props => props.mode === "dark" ? props.theme.colors.black : props.theme.colors.white }; transition-duration: 200ms; } `; export const GlobalStyle = () => { const { theme } = useTheme() return <GlobalStyleComponent mode={theme} /> } 使用 StyledProvider 和 ThemeProvider 包装您的应用程序。 // index.jsx import { StrictMode } from "react"; import ReactDOM from "react-dom/client"; import { StyledProvider } from './context/StyledContext'; import { ThemeProvider } from './context/ThemeContext'; import { GlobalStyle } from './GlobalStyle'; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <StrictMode> <StyledProvider> <ThemeProvider> <GlobalStyle/> <App> </ThemeProvider> </StyledProvider> </StrictMode> )

回答 1 投票 0

ReactJS导入组件Cron覆盖样式

我注意到我的ReactJS应用程序改变了样式,尤其是当我从react-cron-generator导入Cron组件后的字体: 从“react-cron-generator”导入 Cron 调查...

回答 1 投票 0

无法使用react-phone-input-2中的样式化组件覆盖样式

样式可以用导入的附加 CSS 文件覆盖,但不能用样式组件覆盖。为什么? 从“反应”导入反应; 从“styled-components/macro”导入样式; 导入

回答 2 投票 0

防止 CSS 动画在 Gatsby Hydration 上重复

在 gatsby 中,我有一个元素,它有一个初始的 css 动画。 当静态网站加载时,它开始播放得很好,但在水合作用后,它会重复播放。 有什么办法可以预防吗? 这是我的风格...

回答 1 投票 0

如何使用React查询获取数据并过滤?

当我尝试应用过滤数据功能时,出现错误显示,我不知道解决方法,我向聊天 gpt 和 google bard 询问,但没有任何结果。 这是代码和错误屏幕...

回答 1 投票 0

如何查找哪个类名属于哪个元素

我正在开发一个使用样式组件的 React 应用程序。有时真的很难分辨样式来自哪里。 (因为类名只是随机字母)。有没有...

回答 1 投票 0

Nextjs 14 上的 Styled-Components 的 ThemeProvider 和 prop 主题

我在使用样式组件生成主题时遇到一些问题。该错误乍一看似乎是一个简单的修复,但我无法弄清楚问题出在哪里。 NextJS 错误 理论上我想我有

回答 1 投票 0

CSS 按钮在 Webkit 中呈现不同

我在 div 内有一个宽度可变的按钮,我面临的问题是该按钮仅在 Safari 中以不同的方式呈现。 在所有浏览器中它呈现如下: 但在 Safari 中(...

回答 1 投票 0

如何安全地将React版本从16升级到18?

当依赖项之一“styled-components-modifiers”当前使用已知与...兼容的最新版本 1.2.5 时,如何安全地将我的 React 版本从 16 升级到 18

回答 1 投票 0

React Native FlatList、样式化组件和 Typescript

我正在与 styled-components 、 FlatList 和 typescript 进行一场历史性的战斗...... 到目前为止,我已经能够使其与以下内容一起工作: const StyledList = styled(FlatList as new () =...

回答 2 投票 0

在React中将大数据加载为网格和表格(使用react-window)

我有一个带有起始页的项目,它显示用户的整个项目列表。 用户可以在网格和表格之间切换。 对于网格,我使用 CSS 网格 显示:网格; 网格模板列:

回答 1 投票 0

使用带有 props 和 TypeScript 的样式组件

我正在尝试将 TypeScript 集成到我们的项目中,到目前为止,我偶然发现了样式组件库的一个问题。 考虑这个组件 从“react”导入* as React; 进口麦粒肿...

回答 9 投票 0

使用样式组件和React,如何在输入上制作浮动标签?

我无法使标签浮动到输入中的正确位置。好吧,当我向输入元素添加占位符时,输入值时标签就会到达它应该在的位置,这

回答 1 投票 0

如何在样式组件中使用 props

我想将一个url传递给一个组件,然后使用这个url来设置组件的背景图片。 我还想检查网址是否为空。 成分 : css(样式): `常量

回答 1 投票 0

“next/font”需要 SWC,尽管由于存在自定义 babel 配置而正在使用 Babel

当我尝试使用样式组件运行 Nextjs 项目时,我遇到了此错误。 这是我的 .babelrc { “插件”:[ [ “babel-插件风格的组件”, { ...

回答 2 投票 0

React 样式组件 TypeScript 使用基本样式上的 props 扩展样式出现 TS2769 错误

我试图将基本样式扩展到其他样式组件,但出现打字稿错误。我已经能够在纯 javascript 中执行此操作,但无法在 Typescript 中执行相同操作 基础文件 ...

回答 1 投票 0

动画后应用背景滤镜样式

为了进一步了解React,我使用react-router-dom、styled-components和framer-motion创建了一个项目。 我的版本是这样的。 包-json: .... “反应”:“^18.2....

回答 2 投票 0

样式组件插值函数中出现“函数缺少返回类型”错误

用于从道具中提取主题数据的插值函数会产生“函数缺少返回类型”。 eslint 错误。我已使用声明合并来输入我的主题数据,如

回答 2 投票 0

样式组件使用计算功能

我觉得这应该不会太难,但经过一番谷歌搜索后我似乎无法弄清楚。我正在尝试根据带有另一个 scss 变量的计算函数设置项目的宽度。

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.