- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这更像是一个 Typescript
和 React
问题,但为了全面披露,我会提到我在尝试使用 react-navigation 创建自定义 header 时结束了这里。 .
我有一个简单的 ImageButton
(功能)组件,它接受以下 Prop :
export interface ImageButtonProps extends TouchableOpacityProps {
transparent?: boolean;
}
我的自定义标题看起来(大致)是这样的:
export default function Header(props: StackHeaderProps) {
const options = props.scene.descriptor.options;
const HeaderRight = options.headerRight as (() => ReactElement<ImageButtonProps>);
return (<HeaderRight transparent={true}/>); // <-- Typescript complains here
}
可以安全地假设
options.headerRight
将始终返回
ImageButton
,这实际上呈现得很好。唯一的问题是属性
transparent
始终未定义,并且 Typescript 也会引发以下错误:
TS2322: Type '{ transparent: boolean; }' is not assignable to type 'IntrinsicAttributes'. Property 'transparent' does not exist on type 'IntrinsicAttributes'.
ReactElement
到我的自定义组件,
ImageButton
和它的 Prop ?
最佳答案
您的错误是因为您将其转换为的类型不带任何参数或 Prop 。
这意味着,一个不带参数的函数返回 React.Element:
() => ReactElement<ImageButtonProps>
你想让它拿 Prop ,所以你需要输入 Prop :
(props: ImageButtonProps) => ReactElement<any>
或者
React.ComponentType<ImageButtonProps>
此外,TypeScript 不支持对 ReactElement 进行类型检查,因此无需执行
ReactElement<ImageButtonProps>
.
关于reactjs - ReactElement 类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65810593/
将 ReactElement 作为属性传递有什么区别: First case } /> function RenderParam({ ReactElement }) { return {React
这更像是一个 Typescript和 React问题,但为了全面披露,我会提到我在尝试使用 react-navigation 创建自定义 header 时结束了这里。 . 我有一个简单的 ImageB
PanResponder 方法被传递给一个 ResponderSyntheticEvent,它的 nativeEvent.target 是一个 React 标签(它只是一个 Number)。这与 fi
我目前正在寻找通过循环通过该组件所有者通过 props 传递的数组来渲染类中的多个元素。这是一个例子: render() { return ( {th
我在 React 中有一个非常简单的 If 组件: 'use strict'; var React = require('react'); module.exports = React.createC
我正在读一篇section of the React docs它使用以下词汇,但我无法弄清楚它们之间的关系: 一个组件 组件的实例 组件的支持实例 虚拟 DOM 元素 一个ReactElement 它
我正在尝试使用 sweetalert-react 包 ( https://github.com/chentsulin/sweetalert-react ) 作为我的应用程序的模态。现在我让它工作了,但
在我的 JSX 中,我有一个条件渲染逻辑的情况 - 当元素 A 渲染某些东西时(它的 render() 函数返回 null 以外的东西),然后还渲染元素 B,就在元素 A 的上方。 代码示例(简化)如
我正在尝试将 Flow 与 ReactJS 结合使用。我需要为 prop children 添加类型。文章 ( Flow + React) 对此没有任何说明。 我发现了很多使用 ReactElemen
这段代码: import * as React from 'react';
我正在尝试添加这个简单的 div到我的 .tsx 文件的返回块: 我这样做: import React from 'react'; import Radium from 'radium'; impo
基本问题: 类型 React.ReactElement 与 JSX.Element 之间有什么区别?它们相同吗?我什么时候应该使用它们? 最佳答案 尽管 JSX 可以在 React 中使用来创建 Re
我有一个看起来像这样的组件。此版本完美运行: export default function StatusMessage(isAdded: boolean, errorMessage: string)
我有简单的Paragraph组件设置如下 import * as React from 'react'; import {Text} from 'react-native'; const { memo
我目前正在将 React 应用程序迁移到 TypeScript。到目前为止,这工作得很好,但我的 render 函数的返回类型有问题,特别是在我的功能组件中。 我一直使用 JSX.Element 作为
我有一个非常简单的测试 /** @jsx React.DOM */ var store = require('../simpleStore'); var TestUtils = require('re
当使用 React.SFC 时,typescript 出现以下错误: Type '({ children }: { children?: ReactNode; }) => Element' is no
我在 node_module 中收到多个 React.js 类型错误在构建步骤中: TS2320: Interface 'Element' cannot simultaneously extend t
我正在尝试将我的 App 组件传递到我的 Body 组件。它可以正常工作,而且似乎渲染得很好,但我在控制台中收到上述警告。这是我的代码: Document.js import React from '
我正在尝试使用 react v4 和 axios 发布帖子,我关注的是 this tutorial我完全按照他说的做了,但我收到了一个错误:可能是因为我使用的是其他版本的 react,请问如何解决?
我是一名优秀的程序员,十分优秀!