Open In App

How to Create Phone numbers and Contacts List in ReactJS ?

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:

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:

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.




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


Article Tags :