React is a popular JavaScript library for building user interfaces. One of the fundamental concepts in React is the use of components and props. Components allow developers to divide the UI into small, reusable pieces, while props enable communication and data flow between these components.
In this article, we will delve into the concept of components, how to create them, and how to pass props from one component to another.
Components are the building blocks of React applications. They are self-contained, reusable pieces of code that can be composed together to create complex user interfaces. Components can be of two types: functional components and class components.
Functional components are stateless and are defined as JavaScript functions. They take props as input and return JSX (JavaScript XML) elements that describe the UI.
Example:
import React from 'react';
const Welcome = (props) => {
return <h1>Hello, {props.name}!</h1>;
};
export default Welcome;
Class components are stateful and are defined as ES6 classes. They inherit from the `React.Component` class and can maintain their state using `this.state`.
Example:
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return <p>Count: {this.state.count}</p>;
}
}
export default Counter;
To create a component, we use the `React.createElement` or JSX syntax. JSX provides a more declarative way of defining components and is widely used in the React community.
Example of JSX:
import React from 'react';
const Greeting = (props) => {
return <h2>Welcome, {props.name}!</h2>;
};
export default Greeting;
Props (short for properties) allow data to be passed from parent components to child components. They are read-only and help maintain the immutability of data in React.
import React from 'react';
import Greeting from './Greeting';
function App() {
const name = 'John';
return (
<>
<Greeting name={name} />
</>
);
}
export default App;
import React from 'react';
const Greeting = (props) => {
return <h2>Welcome, {props.name}!</h2>;
};
export default Greeting;
In this example, the parent component `App` passes the `name` prop to the child component `Greeting`. The child component then uses this prop to display a personalised greeting.
Final Result:
You can clone and Run the complete code available in Github in passing-props branch.
Components can be nested inside each other to create complex UI structures. Each component can pass props to its children, allowing data to flow down the component tree.
Example:
import React from 'react';
const Book = (props) => {
return (
<div>
<h3>Title: {props.title}</h3>
<p>Author: {props.author}</p>
</div>
);
};
const Bookshelf = () => {
return (
<div>
<h2>My Bookshelf</h2>
<Book title="React in Action" author="Mark T. Thomas" />
<Book title="JavaScript: The Good Parts" author="Douglas Crockford" />
</div>
);
};
export default Bookshelf;
In this example, the `Bookshelf` component nests the `Book` component twice, passing different props to each instance of the `Book` component.
Final Result:
You can clone and Run the complete code available in Github in nested-component branch.
Components and props form the core of React development. Understanding how to create components and pass data between them using props is essential for building scalable and maintainable React applications. By breaking down complex UIs into smaller, reusable components, React empowers developers to create dynamic and interactive user interfaces with ease. So, go ahead and experiment with components and props in your React projects, and unlock the full potential of this powerful library.
Happy coding!