I have written a utility function that I like but for some reason I am unable to get achieve the flow typing for it. The below code is producing errors.
// @flow
import React from 'react';
import type { Node } from 'react';
export const partializeComponent = (partialProps: any) =>
(Component: Node) =>
(props: any): Node => (
<Component
{...partialProps}
{...props}
/>
);
Since the error was very verbose, I have taken a screenshot instead
A React Node is one of the following types: Boolean (which is ignored) null or undefined (which is ignored) Number. String.
What are props in React? We use props in React to pass data from one component to another (from a parent component to a child component(s)). Props is just a shorter way of saying properties. They are useful when you want the flow of data in your app to be dynamic.
React.Node can be null, a boolean, a number, a string, a React element, or an array of any of those types recursively.
Your problem is you're using the type Node
incorrectly when applied to your argument component. The type Node
represents a JSX element e.g. .... It is the correct return type for a React components render method.
In fact you should use the type ComponentType
and a pass a Prop type to reflect the component's implementation.
I've updated your example, filled in some blanks for you to get on your way.
// @flow
import React from 'react';
import type { ComponentType, Node } from 'react';
type PartialProps = {
prop1: string,
prop2: number,
};
type Props = PartialProps & {
otherProps: string,
};
export const partializeComponent = (partialProps: PartialProps) =>
(Component: ComponentType<Props>) =>
(props: Props): Node => (
<Component
{...partialProps}
{...props}
/>
);
Please note this is not tested, it's from memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With