流型-高阶零件-道具注入

时间:2018-07-12 08:26:41

标签: reactjs flowtype

我尝试使用高阶分量进行道具注入,但我做对了。

请问为什么这不正确?

/* @flow */

import * as React from 'react';

type FooType = {
  foo: string, 
  bar: string
};

const Foo = ({ foo, bar }: FooType)=> <div>{foo}{bar}</div>;

const addBlah = <T: {}>(Component: React.ComponentType<T & { bar: string }>) => 
        ({...props}) => <Component {...props} bar={"bar"}/>;

const WithBlah = addBlah(Foo);

const result = ()=> <WithBlah foo="foo" />;

TryFlow

1 个答案:

答案 0 :(得分:1)

正确的说,与文档一起使用似乎返回类型应包含一个$Diff以及一个可选的void类型。

这是它的样子

/* @flow */

import * as React from 'react';

type FooType = {
  foo: string, 
  bar: string
};

const Foo = ({ foo, bar }: FooType)=> <div>{foo}{bar}</div>;

const addBlah = <T: {}>(Component: React.ComponentType<T>): React.ComponentType<$Diff<T, { bar: string | void }>> => 
        ({...props}) => <Component {...props} bar={"bar"}/>;

const WithBlah = addBlah(Foo);

const result = ()=> <WithBlah foo="foo" />;