Reactjs Flow(0.58+;)如何定义使用withRouter包装组件时的子道具
使用Flow 0.58+、React和Reactjs Flow(0.58+;)如何定义使用withRouter包装组件时的子道具,reactjs,react-router,flowtype,Reactjs,React Router,Flowtype,使用Flow 0.58+、React和React router dom,定义用withRouter包装的组件的道具子级的正确方法是什么 目前,我有一个奇怪的行为,当我试图指定用withRouter包装的组件的子级的类型时,我想了解这个行为 让我们假设一个RouterAware组件: import React, { type Node } from 'react'; import { withRouter } from 'react-router-dom'; type Props = { c
React router dom
,定义用withRouter
包装的组件的道具子级的正确方法是什么
目前,我有一个奇怪的行为,当我试图指定用withRouter
包装的组件的子级的类型时,我想了解这个行为
让我们假设一个RouterAware
组件:
import React, { type Node } from 'react';
import { withRouter } from 'react-router-dom';
type Props = {
children: Node
};
const RouterAware = (props: Props) => <div>{props.children}</div>;
export default withRouter(RouterAware);
错误模板为:
Error: src/js/Components/RouterAware.js:8
8: const RouterAware = (props: Props) => <div>{props.children}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function. This type is incompatible with
137: | React$StatelessFunctionalComponent<Props>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object type. See lib: /private/tmp/flow/flowlib_116f8cac/react.js:137
Callable property is incompatible:
8: const RouterAware = (props: Props) => <div>{props.children}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function. This type is incompatible with
122: (props: Props, context: any): React$Node,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: /private/tmp/flow/flowlib_116f8cac/react.js:122
This parameter is incompatible:
8: const RouterAware = (props: Props) => <div>{props.children}</div>;
^^^^^ object type. This type is incompatible with
144: Component: React$ComponentType<{| ...ContextRouter, ...P |}>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object type. See lib: ../../../flow-typed/npm/react-router-dom_v4.x.x.js:144
Property `children` is incompatible:
5: children: Node
^^^^ <Specific union type here>. This type is incompatible with
35: <RouterAware>
^^^^^^^^^^^^^ React children array. See: src/js/Root.js:35
流错误出现在带有消息div的子节点[]
上,类型与对象类型数组不兼容。
这是否是预期的行为,当使用路由器时,应该将其子节点定义为Node[]
(即使这没有什么意义,因为我需要子节点:Node | Node[]
来处理这两种情况)
还是这是一只虫子?我认为这与React组件如何在多个props.children=[,]
时将子元素转换为数组或在单个props.children=
时将单个元素有关
但即使在那里,为什么它只在路由器上出现。如有任何解释,将不胜感激。您能为路由器添加的类型吗?你能复制一下吗?
declare type React$Node =
| void
| null
| boolean
| number
| string
| React$Element<any>
| React$Portal
| Iterable<React$Node>;
Error: src/js/Components/RouterAware.js:8
8: const RouterAware = (props: Props) => <div>{props.children}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function. This type is incompatible with
137: | React$StatelessFunctionalComponent<Props>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object type. See lib: /private/tmp/flow/flowlib_116f8cac/react.js:137
Callable property is incompatible:
8: const RouterAware = (props: Props) => <div>{props.children}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function. This type is incompatible with
122: (props: Props, context: any): React$Node,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: /private/tmp/flow/flowlib_116f8cac/react.js:122
This parameter is incompatible:
8: const RouterAware = (props: Props) => <div>{props.children}</div>;
^^^^^ object type. This type is incompatible with
144: Component: React$ComponentType<{| ...ContextRouter, ...P |}>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object type. See lib: ../../../flow-typed/npm/react-router-dom_v4.x.x.js:144
Property `children` is incompatible:
5: children: Node
^^^^ <Specific union type here>. This type is incompatible with
35: <RouterAware>
^^^^^^^^^^^^^ React children array. See: src/js/Root.js:35
const Root = () => (
<RouterAware>
<div>One</div>
</RouterAware>
);