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
?
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.
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.
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 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!
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.
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.
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. ...
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:
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.
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