top of page

Introduction to Components in React

Components are the building blocks of any React app. Components allow us split the UI into independent and reusable pieces.

A component is combination of


1. Template using HTML

2. User Interactivity using JS

3. Applying Styles using CSS


A typical React app will have many components like header component, navbar component, footer component and content component. Conceptually a component is a JavaScript class or function that accepts inputs which are properties(props) and returns a React element that describes how a section of the User Interface should appear. A component can be created as Function Component or Class Component.

Open the demo-project using VS code and run this Project using Nodejs Command Prompt. Lets say we want to Create a Javascript function which takes an Object and returns a container which displays Employee Information.


function Employee(data) {
  return <div><p>Name : {data.name}</p>
    <p>Salary : {data.salary}</p></div>;
}

The above function is an example of Component and we can Call this by writing


const element = <Employee name="Sara" salary="12345" />;

and this element can be rendered using


ReactDOM.render(element,  document.getElementById('root'));

In our Previous sessions, we have created React elements that represent DOM tags. However, elements can also represent user-defined components. When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object “props”.

A function can also be created using Arrow function. Lets modify the same code using Arrow function.


const Employee=(data)=> {
  return <div><p>Name : {data.name}</p>
    <p>Salary : {data.salary}</p></div>;
}

Note: Always start component names with a capital letter. React treats components starting with lowercase letters as DOM tags. For example, <div /> represents an HTML div tag, but <Employee /> represents a component. Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail.

Now lets Say along with the Employee Details, we would like to Display Employee Department information as well. One way is to write the Code to display department information in the Employee Component. It is not a good Practice to keep everything in one component. To promote Code reusability, we will split them into different components. So lets create a New Department Component which will display Department Information and this Component can be used by any other component.


const Employee=(data)=> {
  return (<div><p>Name : {data.name}</p>
    <p>Salary : {data.salary}</p>
  <Department dept={data.dept} head={data.head}/>
  </div>);
}

const Department=(deptInfo)=>{
  return <div><p>Dept Name is : <b>{deptInfo.dept}</b></p>
    <p>Dept Head is : <b>{deptInfo.head}</b></p>
    </div>;
}

const element = <Employee name="Sara" salary="12345" dept="Test" head="Head" />;

ReactDOM.render(element,  document.getElementById('root'));

Related Posts

See All

Use Props in React

To use props in React, you need to follow these steps: 1. Define a component that accepts props: Start by creating a component that expects to receive props as input. This can be either a functional c

React JS forms

Forms are really important in any website for login, signup, or whatever. It is easy to make a form in HTML but forms in React work a little differently. In HTML the form data is usually handled by

Comments


bottom of page