【问题标题】:Use props to set styled components CSS rules in TypeScript project在 TypeScript 项目中使用 props 设置样式组件 CSS 规则
【发布时间】:2019-03-21 00:41:40
【问题描述】:

使用以下代码:

import * as React from 'react';
import styled from 'styled-components';

interface InnerSectionContainerProps {
    // tslint:disable-next-line:no-any
    children: any;
    inner: boolean;
}

const Container = styled.div`
  ${({ inner }) => inner && `
    max-width: 1150px;
    margin: 0px auto 0 auto;
  `}
`;

export default function InnerSectionContainer(props: InnerSectionContainerProps) {
    return (
        <Container inner={props.inner}>
            {props.children}
        </Container>
    );
}

我得到这个错误:

[ts] Type 'ThemedStyledProps<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, any>' has no property 'inner' and no string index signature.

如何更改此代码才能正常工作?

【问题讨论】:

    标签: reactjs typescript styled-components


    【解决方案1】:

    按照this section of the documentation的第二个例子:

    const Container = styled<InnerSectionContainerProps, "div">("div")`
      ${({ inner }) => inner && `
        max-width: 1150px;
        margin: 0px auto 0 auto;
      `}
    `;
    

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 1970-01-01
      • 2021-06-17
      • 2021-06-16
      • 2021-02-23
      • 2021-08-02
      • 2021-07-26
      • 2019-09-25
      • 2019-04-11
      相关资源
      最近更新 更多