简体   繁体   English

如何将自定义属性传递给TypeScript + StyledCompoments?

[英]How do you pass custom properties to TypeScript + StyledCompoments?

I'm getting the following error when trying to use Styled Compoments 3 with TypeScript: 尝试将Typed Compoments 3与TypeScript一起使用时出现以下错误:

TS2365: Operator '<' cannot be applied to types 'ThemedStyledFunction<{}, any, DetailedHTMLProps<TableHTMLAttributes<HTMLTableElement>, HTMLTableE...' and '{ bkgImg: any; }'.
TS2365: Operator '>' cannot be applied to types 'boolean' and 'string'.
TS2693: 'string' only refers to a type, but is being used as a value here.
TS2604: JSX element type 'ContainerTable' does not have any construct or call signatures.

Hero.tsx Hero.tsx

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


const ContainerTable = styled("table")<{ bkgImg: string }>`
  background-image: url(http://baseurl.com/${(props) => props.bkgImg});
`;

 ....

export const Hero = ({
  heading,
  bkgImg,
}) => (
  <ContainerTable
    bkgImg={bkgImg}
  >
    <tr>
      <ContentTd>
        <table>
          <tr>
            <td>
              <H1>
                {heading}
              </H1>
            </td>
            <td>

            </td>
          </tr>
        </table>
      </ContentTd>
    </tr>
  </ContainerTable>
);

I'm calling this component like so: 我这样称呼这个组件:

    <Hero
      bkgImg="image.png"
      heading={
        <div>
          Way to go! <br />
        </div>
      }
    />

I'm using: 我正在使用:

"styled-components": "3.3.2",
"react": "16.6.3",

What am I doing wrong here? 我在这里做错了什么?

How about: 怎么样:

import React from "react";
import styled from "styled-components";

const ContainerTable = styled.table`
  background-image: url(http://baseurl.com/${(props: { bkgImg: string }) => props.bkgImg});
`;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何将 React 回调 function 传递给 typescript object? - How do you pass a React callback function to a typescript object? 在 Typescript 中,如果事先不知道对象的属性,您如何键入它? - In Typescript, how do you type an object when it's properties are not know beforehand? TypeScript React:如何将泛型传递给 React.ComponentProps<typeof ComponentWithGenericProps> ? - TypeScript React: How do you pass a generic to a React.ComponentProps<typeof ComponentWithGenericProps>? 如何在 typescript 中的反应自定义挂钩中传递参数? - How to pass an argument in a react custom hook in typescript? 在 React 中,使用 TypeScript,如何使用 RefForwardingComponent 和 forwardRef 将 ref 传递给自定义组件? - In React, using TypeScript, how do I pass a ref to a custom component using RefForwardingComponent and forwardRef? 如果你想要所有 css 属性作为 typescript 中的一个类型,你放什么? - if you want all css properties as a type in typescript what do you put? 如何将其他属性传递给自定义反应选择组件? - How do I pass additional properties to a custom react-select component? 使用 typescript 将属性传递给 React 组件 - Pass properties to React components with typescript 如何将 TypeScript 与 withRouter、connect、React.Component 和自定义属性一起使用? - How to use TypeScript with withRouter, connect, React.Component and custom properties? TypeScript - 如何添加评论? - TypeScript - How do you add comments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM