React Native 中没有重载与此调用错误匹配

问题描述 投票:0回答:1

问题

以下代码会引发错误。该代码是重现原始错误的最小可执行代码。

import React from 'react';
import { Button, StyleSheet } from 'react-native';

const MyButton = () => {
  return (
    <Button
      title="Click me"
      style={styles.button}
    />
  );
};

const styles = StyleSheet.create({
  button: {
    backgroundColor: '#0f0',
    color: '#fff',
    padding: 10,
  },
});

错误:

No overload matches this call.
  Overload 1 of 2, '(props: ButtonProps): Button', gave the following error.
    Type '{ title: string; style: { backgroundColor: string; color: string; padding: number; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps>'.
      Property 'style' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps>'.
  Overload 2 of 2, '(props: ButtonProps, context: any): Button', gave the following error.
    Type '{ title: string; style: { backgroundColor: string; color: string; padding: number; }; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps>'.
      Property 'style' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps>'.ts(2769)

结果,React Native 网页是空的。

环境

  • Windows 11
  • npm 10.8.2

到目前为止我做了什么

我检查了以下几页,但没有成功。

属性“样式”不存在...
javascript typescript windows react-native
1个回答
0
投票
错误准确地告诉您问题是什么。

React Native 中的 Button 组件没有 style 属性。

“没有与此调用匹配的重载”意味着根据组件的类型,您正在传递一个不存在的属性或属性组合。

© www.soinside.com 2019 - 2024. All rights reserved.