【问题标题】:Styled components - Styles in component won't apply until I refresh. Refreshing one component breaks styles of another样式化的组件 - 组件中的样式在我刷新之前不会应用。刷新一个组件会破坏另一个组件的样式
【发布时间】:2019-08-18 13:13:43
【问题描述】:

我有一个家庭组件和一个项目组件。使用styled-components,我根据输入道具的项目数量动态设置项目组件中的flexbox属性flex-basis。但是在我刷新组件之前,该样式不会应用。然后,如果我导航回首页,则该组件上的样式会被破坏,直到我刷新为止。

似乎刷新一个组件会破坏另一个组件的样式。

我正在使用带有 React 前端预设的 Laravel。

反应代码:

<ProjectsWrapper projects={projects}>

样式化组件代码:

${breakpoints.tablet`
   .card {
      flex-basis: ${props => props.projects < 3 ? '50%' : '33%'};
   }
`};

更新为显示路由:

return (
            <SiteWrapper>
                <div className="container-fluid">
                    <Router>
                        <Switch>
                            <Route
                                render={({ location }) => (

                                (more code such as navigation...)

                                <div style={{ position: 'relative' }}>
                                            {routes.map(({ path, Component }) => (
                                                <Route key={path} exact path={path}>
                                                    {({ match }) => <Component data={dataObject} imagepath={imagePath} in={match != null} />}
                                                </Route>
                                            ))}
                                        </div>
                                    </div>
                                )}
                              />
                        </Switch>
                    </Router>
                </div>
            </SiteWrapper>
)

它所映射的“路线”是一个包含以下内容的对象:

const routes = [
    { path: '/', name: 'Home', Component: HomePage },
    { path: '/about', name: 'About', Component: AboutPage },
    { path: '/projects', name: 'Projects', Component: ProjectsPage },
    { path: '/blog' , name: 'Blog', Component: BlogPage }
]

【问题讨论】:

  • 刷新组件是什么意思?
  • 我的意思是在该组件上刷新浏览器
  • @xDreamCoding ^
  • 你的路由怎么样?此外,您似乎每次都在渲染中创建样式化组件内联。尝试将其保存到类之外的 const 中,并将其用作渲染中的 jsx-tag
  • @xDreamCoding 我正在我的 react 类之外的 const 中创建我的样式化组件,在导入之后进一步向上。然后将它与 jsx 标签一起使用。我已经更新了帖子以显示我的路由是如何完成的。

标签: reactjs laravel styled-components


【解决方案1】:

所以我不太明白你是如何在这里使用样式化组件的,我不知道 breakpoints.tablet 是什么,但我假设一个样式化组件。

你可以这样修改:

const StyledCardContainer = styled(breakpoints.tablet)`
   .card {
      flex-basis: ${props => props.projects < 3 ? '50%' : '33%'};
   }
`;

在你的 ProjectsWrapper 的渲染中我假设:

<StyledCardContainer projects=${this.props.projects}>
   <div class="card">...</div>
</StyledCardContainer

尽管 CSS 应该直接应用于样式组件 StyledCard,而不是通过类。

【讨论】:

  • breakpoints 是一个带有不同断点的 javascript 文件,即: const breakpoints = { mobile: (...args) => css` @media (min-width: ${theme.breakpoints[0]} ) { ${css(...args)}; } , tablet: (...args) =&gt; css @media (min-width: ${theme.breakpoints[1]}) { ${css(...args)}; } ` 所以样式化的容器应该是样式化的 div。我将进行一些重构,以便卡片也是一个样式组件,看看是否有帮助。我已经恢复使用纯 CSS,问题似乎消失了,所以一定是渲染延迟。
猜你喜欢
  • 1970-01-01
  • 2020-10-19
  • 2021-08-16
  • 1970-01-01
  • 2020-08-30
  • 1970-01-01
  • 2021-04-08
  • 2020-10-27
  • 2021-01-12
相关资源
最近更新 更多