Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to type a React function component with TypeScript without using arrow functions?

Currently I have this React component typed as such:

import React, { FunctionComponent } from "react";

const HelloWorld : FunctionComponent = () => {
  return (
    <div>
      Hello
    </div>
  );
}

export default HelloWorld;

I would not like to use arrow functions and write my component like so:

import React, { FunctionComponent } from "react";

function HelloWorld() {
  return (
    <div>
      Hello
    </div>
  );
}

export default HelloWorld;

Is it possible to type normal functions as FunctionComponent?

like image 537
Breno Calazans Avatar asked Mar 12 '19 00:03

Breno Calazans


People also ask

Should React components be arrow functions?

With the release of ES6 arrow function syntax, you now can use both regular JavaScript function and arrow function syntax when creating components. I would say that using the arrow function for React components is the right choice for most use cases.

Why do we need arrow functions in React?

In short, with arrow functions there is no binding of this . In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions, the this keyword always represents the object that defined the arrow function.

How do you pass a function in TypeScript React?

To pass a function as props in React TypeScript: Define a type for the function property in the component's interface. Define the function in the parent component. Pass the function as a prop to the child component.

Is it good to use arrow functions in render methods?

Is it OK to use arrow functions in render methods? Generally speaking, yes, it is OK, and it is often the easiest way to pass parameters to callback functions. If you do have performance issues, by all means, optimize!

What is the difference between react and typescript?

Functional components with TypeScript You can create functional components in TypeScript just like you would in JavaScript. The main difference is the FC interface, which stands for Function Component. We use this to tell TypeScript that this is a React function component and not just a regular function.

How do I use functional components in typescript?

Functional components with TypeScript. You can create functional components in TypeScript just like you would in JavaScript. The main difference is the FC interface, which stands for Function Component. We use this to tell TypeScript that this is a React function component and not just a regular function.

How to create react functional components with react-typescript?

TypeScript with React Functional Components 1 Getting Started. To get up and running, let’s create a new React-TypeScript project directory with the popular generator, Create React App. 2 Functional Component with Properties. Create a file called Child.tsx with the following starter code. ... 3 React Hooks. ... 4 Conclusion. ...

What is the difference between react FC and react props?

The main difference is the FC interface, which stands for Function Component. We use this to tell TypeScript that this is a React function component and not just a regular function. Optionally, we can add types for the props by defining an interface and passing it to the generic FC. A functional component then typically looks like this:


1 Answers

The FunctionComponent type basically boils down to a function that receives props and returns a ReactElement:

(props: PropsWithChildren<P>, context?: any): ReactElement | null;

So one option would be to type your non-arrow function accordingly:

function HelloWorld(): ReactElement {
  return (
    <div>
      Hello
    </div>
  );
}

The other option (which integrates better with the rest of the TS React ecosystem) is to store your function in a named variable:

interface SomeProps {
  someValue: number
}

const HelloWorld: FunctionComponent<SomeProps> = function HelloWorld({ someValue }) {
  return <div>Hi {someValue}</div>
}

Overall though I would recommend that you just use arrow functions as they offer benefits especially when it comes to JS scopes and the this reference.

like image 102
Jesse Carter Avatar answered Nov 10 '22 01:11

Jesse Carter