Skip to content
Related Articles

Related Articles

Improve Article

How to Create Phone numbers and Contacts List in ReactJS ?

  • Last Updated : 12 Feb, 2021

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.

Creating React Application And Installing Module:

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

npx create-react-app foldername

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

cd foldername

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

App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

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/, you will see the following output.




My Personal Notes arrow_drop_up
Recommended Articles
Page :