In React, it seems that I can declare a functional component or just a function returns a JSX element. What confusing me is I don't know the key difference between these two approaches. Is there anything only one approach can do while another can not?
import React from "react";
type ItemProps = {
id: number
name: string
}
const Item: React.FC<ItemProps> = ({ id, name }) =>
(
<section>
my id is {id}
my name is {name}
</section>
)
const item = ({ id, name }: ItemProps) =>
(
<section>
my id is {id}
my name is {name}
</section>
)
export const Container = () =>
(
<section>
{item({ id: 1, name: "item-1" })}
<Item id={1} name={"item-1"} />
</section>
)
ReactNode is wider, it can be text, number, boolean, null, undefined, a portal, a ReactElement, or an array of ReactNodes. It represents anything that React can render. JSX. Element is an internal hook for Typescript.
JSX is just a syntax extension of JavaScript which allows users to write React components; React is an open source frontend JavaScript library for building complex UIs; it allows the programmers to create complex UIs from smaller components.
FC or React. FunctionComponent provides an implicit definition of children . This means that when you type your component with React. FC , the component automatically accepts children provided to your component. The component in the example above can be used like this: function Container() {
What is JSX? JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.
children
prop, which means even if your component does not allow children, typescript would not complain if you are using React.FC
and the parent passes a children. This does not impact anything at the runtime, but it is better to be more explicit about the children prop. This might be going away in the next version of React.FC
, even now you can use React.VFC
propTypes
, displayName
etc, so they would need to be added explicitly if required.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