简体   繁体   English

如何使用带有TypeScript的React-Native中的样式组件键入无状态功能组件?

[英]How to type Stateless Functional Component using styled-component in React-Native with TypeScript?

Since one week we are using styled-component width React-Native and Typescript in my team project. 从一周开始,我们在团队项目中使用样式组件宽度React-Native和Typescript。

I'm forming myself on it and try to understand how to correctly use it with Typescript. 我正在形成自己,并尝试了解如何正确使用Typescript。

My problem 我的问题

I actually have problem to understand how am I supposed to type a Stateless Functional Component with styled method. 我实际上有问题要理解我应该如何使用样式方法键入无状态功能组件。

Actual code 实际代码

Here is what I have following the doc : 以下是我遵循的文档

const SFCView: SFC<{}> = (props) => {
  return <View style={props.style} />
}

const StyledSFCView = styled(SFCView)`
  width: 100;
  height: 100;
  background-color: green
`;

My Error 我的错误

Typescript says to me that props style does not exist in my type SFC<{}> . 打字稿告诉我,我的类型SFC<{}>中不存在道具样式。 Indeed I defined it nowhere. 实际上我没有定义它。

What I tried 我尝试了什么

I tried to tell typescript that my SFC had those props 我试着告诉打字稿我的SFC有这些道具

const SFCView: SFC<{style: ViewStyle}> = (props) => {
  return <View style={props.style} />
}

But when I use SFCView somewhere else in my code typescript tell me that I have to specify all the props of a View. 但是当我在我的代码打字稿中的其他地方使用SFCView时告诉我,我必须指定View的所有道具。

Does anyone know how to correctly type it ? 有谁知道如何正确输入它?

After some research I found the standard for web with className . 经过一些研究,我找到了带有className的 web标准。

My better solution for the moment 我现在更好的解决方案

import { SFC } from 'react';
import { StyleProp, ViewStyle } from 'react-native';

interface IProps {
 style?: StyleProp<ViewStyle>
}

const SFCView: SFC<IProps> = (props) => {
  return <View style={props.style} />
}

const StyledSFCView = styled(SFCView)`
  width: 100;
  height: 100;
  background-color: green
`;

Advantages: 好处:

  • No more error about style prop in SFCView . SFCView没有关于style道具的SFCView
  • style prop is optional so I have no error when I use StyledSFCView . style prop是可选的,所以当我使用StyledSFCView时没有错误。

Drawback: 退税:

  • style is optional and developper can define it when they use StyledSFCView . style是可选的,开发人员可以在使用StyledSFCView时定义它。

Now this is the last point I have to clarify. 现在,这是我必须澄清的最后一点。

You can write style(MyComponent) , but for that to have an effect, you need to pass the prop className , provided by styled-components , to the final DOM element. 您可以编写style(MyComponent) ,但为了产生效果,您需要将由styled-components提供的prop className传递给最终的DOM元素。

In your case, it should look something like this: 在你的情况下,它应该看起来像这样:

const StyledView = styled(View)`
  width: 100;
  height: 100;
  background-color: green;
`;

const View = ({ className, ... otherPropsYouMightHave }) => (
  // Whatever you have in this component
  <div className={className}>
    // Whatever View needs to render
  </div>
)

Edit: As you mentioned, react-native requires the prop style instead of className 编辑:正如您所提到的, react-native需要prop style而不是className

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

相关问题 Typescript React-native 样式组件中的类型问题 - Type issue in Typescript React-native styled-component React-native:样式组件和 typescript 中的多个样式道具 - React-native: multiple style props in styled-component and typescript 如何使用 react、typescript 和 styled-component 将 onclick 方法从父组件传递给子组件? - How to pass onclick method to child component from parent using react, typescript, and styled-component? 使用 Typescript 响应样式组件,类型错误 - React styled-component with Typescript, types error 如何在React Native的无状态功能组件中扩展Native组件的props TypeScript接口? - How to extend a native's component's props TypeScript interface in a stateless, functional component in React Native? 使用样式组件和 TypeScript 创建组件包装器时遇到问题 - Trouble on creating a Component wrapper using styled-component and TypeScript 如何风格<Link>使用样式组件 - How to Style <Link> using styled-component Typescript 样式组件按钮中的问题 ( React-Native ) - Typescript issue in Styled component Button ( React-Native ) 使用TypeScript创建一个接受数组的React无状态功能组件 - Using TypeScript To Create A React Stateless Functional Component That Accepts Array React typescript 将值从滑块传递到样式组件 - React typescript pass value from slider to styled-component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM