About
A component is a user-defined react element in the React tree.
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.
Conceptually, components are like JavaScript functions. They:
- accept arbitrary inputs (called props)
- return React elements ie:
- that describes what should appear on the screen.
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application. See Conditional rendering
Syntax
Functional vs Class
A valid React component:
- accepts a single props object argument with data and returns a React element.
- has a name starting with a capital letter (In order to detect a DOM tag from a React tag. For example, <div/> represents a DOM tag, but represents a component and requires Welcome to be in scope.
- must return a single root element. Generally, a <div> is added to contain all the others element.
The two below syntax's are equivalent:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Feature available only to function:
class Welcome extends React.Component {
// Optional. Needed to set state
constructor(props) {
super(props); // Always call the base constructor with props.
}
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Feature only available to class
Type
Parent/Child
- What is the React Virtual DOM? Components Tree/Hierarchy - Screen/Root
Hierarchy
see What is the React Virtual DOM? Components Tree/Hierarchy
Management
Create
Jsx
Element are created generally from a JSX expression that compiles to the What is a React Element? function.
createElement
Note that the jsx function element above is equivalent in Native javascript to:
function Welcome(props) {
return React.createElement('h1',{}, `Hello, ${props.name}`);
}
where the createElement function permits to create React element (node in the virtual dom tree).
Design: Extracting Components
A good rule of thumb is that if a part of your UI:
- is used several times (Button, Panel, Avatar),
- or is complex enough on its own (App, FeedStory, Comment),
it is a good candidate to be a reusable component.
Documentation Creation / Workbench
Documentation of Component and creations testing framework are called Workbench. List of Workbench
- at React
- at styleguidist
Workbench:
- https://react-styleguidist.js.org/ -6741- Workbench + documentation ?
Doc:
- Annotation:
Libary
Component
- https://github.com/rebassjs/rebass - React primitive UI components built with styled-system
- https://primer.style/components/ - Github design system
- material-ui.com - React components (Material Design).
- https://github.com/dialogs/dialog-web-components - Set of react components for building messaging applications