styled-components it looks like an unknown prop “items“ is being sent through to the DOM, which wil

在这里插入图片描述

styled-components: it looks like an unknown prop “items” is being sent through to the DOM, which will likely trigger a React console error. If you would like automatic filtering of unknown props, you can opt-into that behavior via <StyleSheetManager shouldForwardProp={...}> (connect an API like @emotion/is-prop-valid) or consider using transient props ($ prefix for automatic filtering.)

遇到如上报错信息

解决 配置shouldForwardProp

0 属性名 $ 起手

1 单独配置

export const FlexDiv = styled.div.withConfig({
    shouldForwardProp: props =>
        !["justify", "items", "space", "column", "flex", "height", "width", "center"].includes(props),
})<{
    justify?: JustifyContentProps;
    items?: AlignItems;
    space?: string;
    column?: boolean;
    flex?: number;
    height?: string;
    width?: string;
    center?: boolean;
}>`
    display: flex;
    justify-content: ${props => props.justify || (props.center && "center")};
    align-items: ${props => props.items || (props.center && "center")};
    flex-direction: ${props => (props.column ? "column" : "row")};
    flex-wrap: wrap;
    ${({ space }) => space && `& > * {margin: ${space};}`};
    ${({ flex }) => flex && `flex:${flex}`};
    ${({ height }) => height && `height:${height}`};
    ${({ width }) => width && `width:${width}`};
`;

2 全局配置 @emotion/is-prop-valid

<StyleSheetManager sheet={styledComponentsStyleSheet.instance} shouldForwardProp={isPropValid}>
    {children}
</StyleSheetManager>
"use client";

import React, { useState } from "react";
import { useServerInsertedHTML } from "next/navigation";
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
import isPropValid from '@emotion/is-prop-valid'

export default function StyledComponentsRegistry({
  children,
}: {
  children: React.ReactNode;
}) {
  const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());

  useServerInsertedHTML(() => {
    const styles = styledComponentsStyleSheet.getStyleElement();
    styledComponentsStyleSheet.instance.clearTag();
    return <>{styles}</>;
  });

  if (typeof window !== "undefined") return <>{children}</>;

  return (
    <StyleSheetManager sheet={styledComponentsStyleSheet.instance} shouldForwardProp={isPropValid}>
      {children}
    </StyleSheetManager>
  );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值