Learn React.js Components and Props — 3

Ahson Shaikh
3 min readOct 24, 2021

--

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) and return React element describing what should appear on the screen.

There are basically two types of components; 1) Functional Components, 2) Class Components.

Functional Components: The simplest way to define a component is to write a JavaScript function:

Functional Component must return a statement.

function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

Class Components: Class components are/can be declared with ES6 class.

Class Component has a method called render() followed by a return statement.

class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}

Important: How React Renders a Component?

Previously, we saw React elements that represent DOM tags:

const element = <div />

But, elements also represent user-defined components:

const element = <Welcome name="Sara" />;

“Welcome” is user-defined component. When react sees an element representing a user-defined component (in our case: const element is representing <Welcome/> component), it passes the JSX attributes and children to this component as a single object. We call this object Props.

For example:

function Welcome(props) {  
return <h1>Hello, {props.name}</h1>;
}

const element = <Welcome name="Sara" />;ReactDOM.render(
element,
document.getElementById('root')
);

Composing Components:

Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail.

For example, we can create an App component that renders Welcome many times:

function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

function App() {
return (
<div>
<Welcome name="Sara" /> <Welcome name="Cahal" /> <Welcome name="Edite" /> </div>
);
}

ReactDOM.render(
<App />,
document.getElementById('root')
);

Extracting Components:

Don’t be afraid to split components into smaller components. For example, consider this comment:

function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<img className="Avatar"
src={props.author.avatarUrl}
alt={props.author.name}
/>
<div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}

It accepts author (an object), text (a string), and date (a date) as props, and describes a comment on a social media website.

This component can be tricky to change because of all the nesting, and it is also hard to reuse individual parts of it. Let’s extract a few components from it.

First, we will extract Avatar:

function Avatar(props) {
return (
<img className="Avatar" src={props.user.avatarUrl} alt={props.user.name} /> );
}

The Avatar doesn’t need to know that it is being rendered inside a Comment. This is why we have given its prop a more generic name: user rather than author.

We recommend naming props from the component’s own point of view rather than the context in which it is being used.

We can now simplify Comment a tiny bit:

function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<Avatar user={props.author} /> <div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}

Next, we will extract a UserInfo component that renders an Avatar next to the user’s name:

function UserInfo(props) {
return (
<div className="UserInfo"> <Avatar user={props.user} /> <div className="UserInfo-name"> {props.user.name} </div> </div> );
}

This lets us simplify Comment even further:

function Comment(props) {
return (
<div className="Comment">
<UserInfo user={props.author} /> <div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}

Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps. 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 extracted to a separate component.

Props are Read-Only:

All React components must act like pure functions with respect to their props.

Pure functions are those which doesn’t tend to change the input. Like accepting two parameters in their parenthesis and changing its value and returning it.

function withdraw(account, amount) {
account.total -= amount;
}

Application UI’s are dynamic and change over time. In the next section we will introduce a new concept of “state”. State allows React components to change their output over time in response to user actions, network responses, and anything else, without violating this rule.

--

--

Ahson Shaikh
Ahson Shaikh

Written by Ahson Shaikh

DevOps Engineer with 4 years of experience in cloud and on-premises environments, specializing in Python automation with Selenium, currently working at Arpatech

No responses yet