Multi-line JSX should always be wrapped in parentheses.
Importing another component
WelcomePage.tsx
Components always return a single node .
If you want to return multiple nodes, wrap them in a Fragment to avoid rendering an unnecessary <div> .
Using the long-form
Fragment
is equivalent to the shorter
We will see in a bit in which situation the long-form <Fragment> is useful.
We can pass data to child components by setting their props (properties)
A prop can have any type, e.g. it can be a JavaScript primitive, an object, or a function
Props can be defined as required or optional
Pass functions as props to listen to events emitted by child components
Our components can be tested using the React Testing Library
It is already pre-installed when using Create React App.
It is a good practice to conduct TDD wherever possible
Let us amend the component such that we can set the name through an name property
Refactor your code such that it is extensible and easy to understand
To render a list of items, we call the
map
function on an array.
In the callback we map the array item to a JSX element.
We can map to an HTML element or to a custom component.
The JSX element returned from map must have a key prop.
This is a special prop used by React internally to distinguish the elements. It must be unique within the list (e.g. an entity ID).
Use a Fragment to return multiple elements from map .
Here we can't use the shorter <>…</> syntax. Only Fragment can take the key prop.
Like HTML elements, React components can be nested:
The parent component receives the child components through the children prop.
The children prop can be rendered in the parent component's JSX.
We learned…