样式组件-CSS类不适用于SSR

时间:2019-12-03 13:19:04

标签: css reactjs styled-components server-side-rendering

使用链接导航到页面时遇到问题,我的CSS类未全部应用。 enter image description here

当我重新加载此页面时,所有类均正确应用。 enter image description here

我认为这与使用带有样式化组件的服务器端渲染有关,但是我不知道下一步该怎么解决。

样式化组件v4.4.1

编辑:

错误链接(按钮组件)的代码

import React from 'react'
import { Box, styled, css, th } from '@smooth-ui/core-sc'
import { darken, lighten, transparentize } from 'polished'
import theme from 'theme'
import Link from './Link'

const ButtonComponent = ({
  variant,
  size,
  children,
  condensed,
  isRow,
  maxWidth,
  ...props
}) => {
  const as = props.to ? Link : 'button'
  const type = as === 'button' ? props.type : null
  return (
    <Box {...props} type={type} as={as}>
      {children}
    </Box>
  )
}

const plainVariant = (bgColor, textColor) => css`
  background-color: ${th(bgColor)};
  color: ${th(textColor)};

  --focus-color: ${darken(0.08, theme[bgColor])};
  --active-color: ${lighten(0.16, theme[bgColor])};

  &:hover {
    color: ${th(textColor)};
    background-color: ${props => darken(0.08, props.theme[bgColor])};
  }

  &:focus {
    box-shadow: 0 0 0 2px ${props => transparentize(0.75, props.theme[bgColor])};
  }
`

const variants = {
  tertiary: plainVariant('orange', 'white'),
}

const Button = styled(ButtonComponent)`
  display: inline-flex;
  min-width: 200px;
  border-radius: 4px;
  border-width: 0;
  cursor: pointer;
  justify-content: center;
  align-items: center;
  text-align: center;
  text-decoration: none;
  user-select: none;
  outline-offset: 2px;
  vertical-align: middle;
  white-space: nowrap;
  font-weight: 500;
  padding: 0 16px;

  &:hover {
    text-decoration: none;
  }

  ${p => variants[p.variant]};
  ${p => sizes[p.size]};
  ${p =>
    /^gradient-*/.exec(p.variant) || p.rippled === false
      ? rippled.none
      : rippled.default};

  flex-direction: ${p => (p.isRow ? 'row' : 'column')};

  ${p =>
    p.condensed &&
    css`
      min-width: auto;
      line-height: 18px;
      padding: 15px;
    `};

  &:disabled {
    pointer-events: none;
    ${p => (disabled[p.variant] ? disabled[p.variant] : disabled.default)};
  }
`

export default Button

0 个答案:

没有答案