将组件作为道具传递时在 Flow 中键入 React 组件
Typing React components in Flow when passing a component as a prop
我想将一个 React 组件作为输入道具传递给另一个 React 组件。我试图将其引用为 React.Component<*, *, *> 但是当我在渲染方法中使用传递的组件时出现错误。这就是我写流程代码的方式。
/* @flow */
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
const Input = props => <div>Yo</div>
type DefaultProps = {
InputComponent: Input
};
type Props = {
InputComponent: React.Component<*, *, *>
};
class App extends Component<DefaultProps, Props, void> {
static defaultProps = {
InputComponent: Input
};
props: Props;
render() {
const { InputComponent } = this.props
return (
<div>
<InputComponent />
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
)
但是在 App 渲染方法中我得到了错误
React element `InputComponent` (Expected React component instead of React$Component)
我应该如何正确输入输入组件?
从 v0.59.0 开始,您应该使用 React.Component。例如:
/* @flow */
import React from 'react';
const Input = props => <div>Yo</div>
type Props = {
InputComponent: React.Component<*, *>
};
class App extends React.Component<Props, void> {
static defaultProps = {
InputComponent: Input
};
render() {
const { InputComponent } = this.props
return (
<div>
<InputComponent />
</div>
)
}
}
Here is a working example for 0.59.0. As mentioned in the comments, there is a description of the changes here.
:: v0.59.0 之前::
您应该使用 ReactClass<*>
而不是 React.Component
。
这是一个working example, and the documentation is here!
/**
* Type of a React class (not to be confused with the type of instances of a
* React class, which is the React class itself). A React class is any subclass
* of React$Component. We make the type of a React class parametric over Config,
* which is derived from some of the type parameters (DefaultProps, Props) of
* React$Component, abstracting away others (State); whereas a React$Component
* type is useful for checking the definition of a React class, a ReactClass
* type (and the corresponding React$Element type, see below) is useful for
* checking the uses of a React class. The required constraints are set up using
* a "helper" type alias, that takes an additional type parameter C representing
* the React class, which is then abstracted with an existential type (*). The *
* can be thought of as an "auto" instruction to the typechecker, telling it to
* fill in the type from context.
*/
type ReactClass<Config> = _ReactClass<*, *, Config, *>;
type _ReactClass<DefaultProps, Props, Config: $Diff<Props, DefaultProps>, C: React$Component<DefaultProps, Props, any>> = Class<C>;
在版本 Flow 0.93.0 上,您可以执行以下操作。
import type { Element as ReactElement } from 'react';
type Props = {
component: ReactElement
}
我想将一个 React 组件作为输入道具传递给另一个 React 组件。我试图将其引用为 React.Component<*, *, *> 但是当我在渲染方法中使用传递的组件时出现错误。这就是我写流程代码的方式。
/* @flow */
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
const Input = props => <div>Yo</div>
type DefaultProps = {
InputComponent: Input
};
type Props = {
InputComponent: React.Component<*, *, *>
};
class App extends Component<DefaultProps, Props, void> {
static defaultProps = {
InputComponent: Input
};
props: Props;
render() {
const { InputComponent } = this.props
return (
<div>
<InputComponent />
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
)
但是在 App 渲染方法中我得到了错误
React element `InputComponent` (Expected React component instead of React$Component)
我应该如何正确输入输入组件?
从 v0.59.0 开始,您应该使用 React.Component。例如:
/* @flow */
import React from 'react';
const Input = props => <div>Yo</div>
type Props = {
InputComponent: React.Component<*, *>
};
class App extends React.Component<Props, void> {
static defaultProps = {
InputComponent: Input
};
render() {
const { InputComponent } = this.props
return (
<div>
<InputComponent />
</div>
)
}
}
Here is a working example for 0.59.0. As mentioned in the comments, there is a description of the changes here.
:: v0.59.0 之前::
您应该使用 ReactClass<*>
而不是 React.Component
。
这是一个working example, and the documentation is here!
/**
* Type of a React class (not to be confused with the type of instances of a
* React class, which is the React class itself). A React class is any subclass
* of React$Component. We make the type of a React class parametric over Config,
* which is derived from some of the type parameters (DefaultProps, Props) of
* React$Component, abstracting away others (State); whereas a React$Component
* type is useful for checking the definition of a React class, a ReactClass
* type (and the corresponding React$Element type, see below) is useful for
* checking the uses of a React class. The required constraints are set up using
* a "helper" type alias, that takes an additional type parameter C representing
* the React class, which is then abstracted with an existential type (*). The *
* can be thought of as an "auto" instruction to the typechecker, telling it to
* fill in the type from context.
*/
type ReactClass<Config> = _ReactClass<*, *, Config, *>;
type _ReactClass<DefaultProps, Props, Config: $Diff<Props, DefaultProps>, C: React$Component<DefaultProps, Props, any>> = Class<C>;
在版本 Flow 0.93.0 上,您可以执行以下操作。
import type { Element as ReactElement } from 'react'; type Props = { component: ReactElement }