Open In App

How to Create Phone numbers and Contacts List in ReactJS ?

Last Updated : 23 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We can create a phone number list along with contact names in ReactJS using the following approach. Material UI for React has this component available for us and it is very easy to integrate. Contact List/ Numbers can be shown in List and tabs can be switched to change the state of data.

Approach:

  • Component and Import Setup:
    • The code establishes a React functional component `App` and imports Material-UI components like `Paper`, `Tab`, and icons such as `PhoneIcon`.
    • It sets up the component state with `useState` for managing the selected tab index (`value`) and the list of phone numbers or contacts (`myList`).
  • Tab Change Logic:
    • The `handleChange` function handles tab changes, checking the selected index.
    • If the index is 0, it updates `myList` with phone numbers; if 1, it updates with contact names, also updating the `value` state accordingly.
  • Rendering Tabs and List:
    • The JSX renders a Paper container with Tabs displaying phone and person icons for tab switching.
    • An unordered list (`ul`) renders list items (`li`) showing elements from `myList` based on the selected tab.
  • Styling and Presentation:
    • Inline styles are applied to center the component horizontally with a margin-left of 40%.
    • The code showcases a simple React UI demonstrating dynamic content based on tab selection, integrating Material-UI components.

Prerequisites:

Steps to Create the React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app myapp

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd myapp

Step 3: After creating the ReactJS application, Install the material-ui modules using the following command:

npm install @material-ui/core
npm install @material-ui/icons

Project Structure:

reactProjstructure

Project Structure

The updated dependencies in package.json file will look like:

  "dependencies": {
"@material-ui/core": "^4.12.4",
"@material-ui/icons": "^4.11.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Example: Now write down the following code in the App.js file.

Javascript




import React, { useState } from "react";
import Paper from "@material-ui/core/Paper";
import Tab from "@material-ui/core/Tab";
import Tabs from "@material-ui/core/Tabs";
import PhoneIcon from "@material-ui/icons/Phone";
import PersonPinIcon from "@material-ui/icons/PersonPin";
 
const App = () => {
    const [value, setValue] = React.useState(0);
 
    const [myList, setMyList] = useState([
        "999XXXXXXX",
        "8575XXXXXX",
        "99XXXXXXXX",
    ]);
 
    const handleChange = (event, newValue) => {
        if (newValue == 0) {
            setMyList(["999XXXXXXX", "8575XXXXXX", "99XXXXXXXX"]);
            setValue(0);
        } else {
            setMyList(["Contact One", "Contact Two", "Contact Three"]);
            setValue(1);
        }
    };
 
    return (
        <div
            style={{
                marginLeft: "40%",
            }}>
            <h2>
                How to Create Phone numbers and
                Contacts List in ReactJS?
            </h2>
            <Paper
                square
                style={{
                    flexGrow: 1,
                    maxWidth: 500,
                }}>
                <Tabs
                    value={value}
                    onChange={handleChange}
                    variant="fullWidth"
                    indicatorColor="primary"
                    textColor="primary"
                    aria-label="icon tabs example">
                    <Tab icon={<PhoneIcon />} aria-label="phone" />
                    <Tab icon={<PersonPinIcon />} aria-label="person" />
                </Tabs>
                <ul>
                    <li>{myList[0]}</li>
                    <li>{myList[1]}</li>
                    <li>{myList[2]}</li>
                </ul>
            </Paper>
        </div>
    );
};
 
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads