Understanding React Components

Components in React let you split the page into independent and reusable parts.

Let’s visualize this by taking a look at the diagram below.

Notice that the page can be split into multiple parts.
Each of these “parts” is a component.
The heading is …


This content originally appeared on DEV Community and was authored by πƒπšπ§π«πžπ³

Components in React let you split the page into independent and reusable parts.

Let's visualize this by taking a look at the diagram below.

Image description

Notice that the page can be split into multiple parts.
Each of these "parts" is a component.
The heading is a component, the "new question" button is a component, and the search bar is its component.
This makes organizing the page leaps and bounds easier, but even more importantly, components allow us as developers to separate concerns from one another.

Separation of concerns is a programming principle that states that each concern should be separated into individual pieces.
For example, in the diagram above, the "new question" button (2) should be clicked if a user wanted to add a new question, whereas the search bar (3) would be used if the user wanted to search existing questions.

Functional Components

In React, there are two types of components that you can use: Functional Components and Class Components.

A functional component is a simple JavaScript function.

function Hello() {
  return <h1>Hello world.</h1>
}

The code above defined a functional component called Hello, which returns a simple React element.

Rendering Components

We need to create the corresponding JSX element to display the component.
For example, for our user-defined component Hello:

const el = <Hello />;

Now, we can use our user-defined element and render it on the page:

function Hello() {
  return <h1>Hello world.</h1>
}
const el = <Hello />;
ReactDom.reader(
el,
document.getElementById('root')
);

Remember, all component names need to start with a capital letter.

Class Components

Class components are typically used when there are more advanced user interactions, like forms, and animations.

All class components need to extend the React.Component class.

class Hello extends React.Component {
 render() {
   return <h1>Hello world.</h1>;
}
}

Class components need to have a render method, which is in charge of telling what the page should show.


This content originally appeared on DEV Community and was authored by πƒπšπ§π«πžπ³


Print Share Comment Cite Upload Translate Updates
APA

πƒπšπ§π«πžπ³ | Sciencx (2022-02-03T19:25:19+00:00) Understanding React Components. Retrieved from https://www.scien.cx/2022/02/03/understanding-react-components/

MLA
" » Understanding React Components." πƒπšπ§π«πžπ³ | Sciencx - Thursday February 3, 2022, https://www.scien.cx/2022/02/03/understanding-react-components/
HARVARD
πƒπšπ§π«πžπ³ | Sciencx Thursday February 3, 2022 » Understanding React Components., viewed ,<https://www.scien.cx/2022/02/03/understanding-react-components/>
VANCOUVER
πƒπšπ§π«πžπ³ | Sciencx - » Understanding React Components. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/03/understanding-react-components/
CHICAGO
" » Understanding React Components." πƒπšπ§π«πžπ³ | Sciencx - Accessed . https://www.scien.cx/2022/02/03/understanding-react-components/
IEEE
" » Understanding React Components." πƒπšπ§π«πžπ³ | Sciencx [Online]. Available: https://www.scien.cx/2022/02/03/understanding-react-components/. [Accessed: ]
rf:citation
» Understanding React Components | πƒπšπ§π«πžπ³ | Sciencx | https://www.scien.cx/2022/02/03/understanding-react-components/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.