Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow type error when using React.Node

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 insteadenter image description here

like image 257
alaboudi Avatar asked Feb 14 '18 05:02

alaboudi


People also ask

What is type node in React?

A React Node is one of the following types: Boolean (which is ignored) null or undefined (which is ignored) Number. String.

What is the flow of props in React?

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.

Is a string a React node?

React.Node can be null, a boolean, a number, a string, a React element, or an array of any of those types recursively.


1 Answers

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.

like image 111
Jon Miles Avatar answered Sep 22 '22 00:09

Jon Miles