Understanding Components in React

Tram Ho

Prologue

This article does not have a specific goal, it is not an A to Z guide for beginners
It is more like an article that lists documents related to Components, and hopefully somewhere in the article you will find a part you do not know.
If you have a part you don’t understand, don’t hesitate to leave a comment for me!

1. Components

1 Component will be written mainly in [src/features/] and [src/shared/]. Basically a Component will return a piece of html.
folder [src/features/] to save the Components of each feature, like Login, List, Detail… [src/shared/] saves the most used Components like Header, Footer, Input…
Component can be a function or a variable that stores a callBack funcion.
1 Component name MUST start with a capital letter
In Typescript I use code like this:

or shortened callBack function:

Component only allows to return 1 tag, if you want to return more than 1 tag, you need to wrap it with another tag, if it can’t be wrapped, you can use a pair of <></> tags. This pair of tags will disappear when the html code is generated

2. TypeScript + React: Component patterns

This list is a collection of Component templates for React when working with TypeScript.

Basic function components

write Component like a normal function

Props

named after component + props-suffix. No need to use React.FC

Destructuring

same as above but easier to read

Default props

This method allows to set default value for the passed parameter if the Parent Component does not pass the parameter down to Children

Children

instead of using FC or FunctionComponent, we will set [children] type React.ReactNode
since it accepts most (JSX elements, strings, etc.)

how to use Children in Parent Component:

Children are not allowed

instead of using FC or FunctionComponent, we will set [children] type React.ReactNode
since it accepts most (JSX elements, strings, etc.)

WithChildren helper type

instead of using FC or FunctionComponent, we will set [children] type React.ReactNode
since it accepts most (JSX elements, strings, etc.)

This is very similar to FC, but with the default generic parameter {} it can be much more flexible:

If you are using Preact, you can use h.JSX.Element or VNode as a type instead of React.ReactNode.
Personally, I think this part the author is drawing snakes with legs.

3. how a Component is displayed

By default the file is always displayed as index.html, any changes in this file will affect the whole Project. But this is not the place to edit global in React, ngta will try to keep this file from editing

Inside this file there will be 1 tag: <div id="root"></div> . this is where React changes the displayed html content

index.tsx file change this tag with Components via code

in this file there is [import ‘./index.css’;], this is where some global css is applied

The basic App.tsx file is used to categorize each page corresponding to the Url, through the Router

Router.tsx file is usually applied with Extenstion react-router, or manual paging

However, each Component is not a Page, the Component can be as small as a Button, an Area or as large as the App.tsx file, which is also a Component.

Share the news now

Source : Viblo