React Components
React Components
First thing first, before we start off by defining the react component, let’s take a look at the
component definition.
By a simple research on Google, we can find a lot of complicated definitions.
But let’s have an easier one; A component is a piece of code, that can be reusable and cooperate
with other component to create a UI.
Well, let’s see the image below, and how it is structured :
• Reusability
Every implemented component can (if we don’t must) be reused while building our web
application.
Let’s take the example of a sidebar of a dashboard, it will be used whatever the page we display.
• Maintainability
Every component is implemented separately from the others so it can be maintained separately
without affecting other UI composition.
• Platform independency
In reality, the web component are build using HTML, CSS and JavaScript which make it a cross
platform.
• Scoop privacy
Since every component is built to be working alone that make its content more private than shared
code.
This is a functional component (called Greeting) written using ES6’s arrow function syntax. It takes
no props and returns an h1 tag with the text “Hello World today!”
A component is an external library used by react False
The component is a set of code that generate a specific UI True
Component is a JavaScript approach onlyTrue
Types of components
As we said earlier, there are two types in react component, basically that division was before React
version 16.8.
But for the learning purpose we should know it.
The two types of react component are:
• Class based component: referring to JavaScript classes.
• Functional based component: referring to JavaScript function.
By doing that we’ve made the file MyComponent.js export by default the component
MyFirstComponent
Named exports
The other way to export a component is the named export, let’s take a look at the example.
This how we can export more than one component from a single file.
import React from "react";
export const MyFirstComponent = () => {
return (
<>
<h2>Hello from my first component !!</h2>
</>
);
};
Using a Component
After creating our first component, it’s time to use it.
To make it happen, we will return again to JavaScript modularity. In order to use any external
module in our local file, we should first import it.
Well, let’s see an example to make it clear.
MyFirstComponent.js
import React from "react";
App.js
import React from "react";
import MyFirstComponent from "./MyFirstComponent";
const App = () => (
<>
<MyFirstComponent />
</>
);
export default App;
Using a Component
Wouhou! Here, we start making things.
As we can see, we get the component MyFirstComponent inside the App. We may ask the App isn’t
a also a component. Yes, a component can be composed of many other components
const App = () => {
return (
<div>
<Header /> // Header Component
<Footer />
// Footer Component
</div>
);
};
When one component uses another like this, a parent-child relationship is formed. In above example
:
• The App component is the parent to the Header and Footer components.
• The Header and Footer components are the child of the App component.
Notice that, applications built with React have a single root DOM node
Application built with React have many root False
A component can render another component True
To establish a parent child relationship, a component A should render a component B True
What's HOC?
We have seen the higher order function in JavaScript, which is a function that accept a function as a
parameter.
Well, React clone that functionality with component so it gives us the possibility to create a Higher
Order Component which is a component that accept a component as a parameter.
Why HOC?
In software development one of the commonly talked about principle is “Don’t Repeat YourSelf”
aka. DRY. Creating a simple utility function, that is used across several parts of the codebase is
something you may have done frequently. You are essentially following DRY by doing so. You are
reusing the same utility function, without repeating the code.
In React, one of the ways to achieve DRY across components, is to use the Higher Order
Component (HOC) concept. We can share common functionality without repeating code.
Example of HOC
In this example, we will add a waiting message on the wrapped component. Don’t be afraid of the
weird code we'll understand it later.
import React from "react";
render() {
return this.state.isLoading ? (
<h1>Wait a moment...</h1>
) : (
<WrappedComponent />
);
}
}
return HOC;
};