COMPONENTS AND PROPS

Components and Props

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 elements describing what should appear on the screen.

Two types of Components

  1. Class component.
  2. Functional component.

Functional component

Simple functional component example is shown below

const FunctionalComponent = () => {
return (
<div>
<h1>This is functional compunent</h1>
</div>
);
}
export default FunctionalComponent;

Class Components

Example of class component

class Clock extends React.Component {
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.props.date.toLocaleTimeString()}.</h2>
</div>
);
}
}

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

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

This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. We call such components “function components” because they are literally JavaScript functions.

You can also use an ES6 class to define a component :

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

Rendering a Component

Previously, we only encountered React elements that represent DOM tags :

const element = <div />;

However, elements can also represent user-defined components:

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

When React sees an element representing a user-defined component, it passes JSX attributes and children to this component as a single object. We call this object “props”.

For example, this code renders “Hello, Sarah” on the page :

function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sarah" />;
ReactDOM.render(
element,
document.getElementById('root')
);

What happens in this example:

  1. We call ReactDOM.render() with the <Welcome name="Sarah" />element.
  2. React calls the Welcome component with {name: 'Sarah'} as the props.
  3. Our Welcome component returns a <h1>Hello, Sarah</h1> element as the result.
  4. React DOM efficiently updates the DOM to match <h1>Hello, Sarah</h1>.

Note: Always start component names with a capital letter.

Composing Components

Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail. A button, a form, a dialog, a screen: in React apps, all those are commonly expressed as components.

For example, we can create an App component that renders Welcomemany 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')
);

Props are Read-Only

Whether you declare a component as a function or a class, it must never modify its own props. Consider this sum function :

function sum(a, b) {
return a * b;
}

Such functions are called “pure” because they do not attempt to change their inputs, and always return the same result for the same inputs.

In contrast, this function is impure because it changes its own input :

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

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