How to pass data into table from a form using React Components ?
Last Updated :
26 Jul, 2024
React JS is a front-end library used to build UI components. This article will help to learn to pass data into a table from a form using React Components. This will be done using two React components named Table and Form. We will enter data into a form, which will be displayed in the table on 'submit'.
Prerequisites:
Approach:
- App Component:
App.js
renders a basic greeting message and includes the TableData
component.- Serves as the main component orchestrating the application's structure.
- TableData Component:
- Manages and displays student data in a table using state and mapping through the data.
- Integrates a form (
StudentForm
) to add new entries, updating the overall data state.
- StudentForm Component:
- Captures student information through a form with name and city fields.
- Communicates with the parent (
TableData
) using a function prop to transfer new student data.
- Data Handling:
TableData
manages the overall state of student data and updates it when a new student is added.- The
StudentForm
component facilitates the transfer of entered data back to the parent (TableData
).
Steps to create React Application And Installing Module:
Step 1: Create a React application using the following command.
npx create-react-app
Step 2: After creating your project folder(i.e. my-first-app), move to it by using the following command.
cd my-first-app
Step 3: Create a dummy JSON file; that initially contains the following one object and save it as data.json
[ {"id":1,"name":"Akshit","city":"Moradabad"} ]
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"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 respective files.
JavaScript
//App.js
import TableData from "./form";
function App() {
return (
<div className="App">
<h1>Hello Geeks!!!</h1>
<TableData />
</div>
);
}
export default App;
JavaScript
//form.jsx
import React, { useState } from 'react';
import StudentForm from './form';
import jsonData from './data.json';
function TableData() {
const [studentData, setStudentData] = useState(jsonData);
const tableRows = studentData.map((info) => {
return (
<tr>
<td>{info.id}</td>
<td>{info.name}</td>
<td>{info.city}</td>
</tr>
);
});
const addRows = (data) => {
const totalStudents = studentData.length;
data.id = totalStudents + 1;
const updatedStudentData = [...studentData];
updatedStudentData.push(data);
setStudentData(updatedStudentData);
};
return (
<div>
<table className="table table-stripped">
<thead>
<tr>
<th>Sr.NO</th>
<th>Name</th>
<th>City</th>
</tr>
</thead>
<tbody>{tableRows}</tbody>
</table>
<StudentForm func={addRows} />
</div>
);
}
export default TableData;
JavaScript
//table.jsx
import React, { useState } from 'react';
function StudentForm(props) {
const [name, setName] = useState('');
const [city, setCity] = useState('');
const changeName = (event) => {
setName(event.target.value);
};
const changeCity = (event) => {
setCity(event.target.value);
};
const transferValue = (event) => {
event.preventDefault();
const val = {
name,
city,
};
props.func(val);
clearState();
};
const clearState = () => {
setName('');
setCity('');
};
return (
<div>
<label>Name</label>
<input type="text" value={name} onChange={changeName} />
<label>City</label>
<input type="text" value={city} onChange={changeCity} />
<button onClick={transferValue}> Click Me</button>
</div>
);
}
export default StudentForm;
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:
Similar Reads
How to Map Data into Components using ReactJS? Mapping data in react js component is a comman practice to render the lists and repeating elements. In this article, we will have a users data and we will map it to react components to display the users information in the react appPrerequisites:React JSNodejs and npmJavaScript Array mapApproachTo ma
3 min read
How to parse JSON Data into React Table Component ? In React parsing JSON Data into React Table Component is a common task to represent and structure data. We can render the JSON data into a table dynamically using the array map.Prerequisites:NPM & Node JSReact JSJSON parse()ApproachTo render JSON data in React Table we will be using the JavaScri
2 min read
How to Pass Data From Child Component To Its Parent In ReactJS ? In ReactJS, the flow of data is typically one-way, meaning data is passed from parent to child components using props. However, there are situations where you may need to pass data from a child component back up to its parent component. In this article, we will cover how to pass data from a child co
5 min read
How to Pass Data from One Component to Another Component in ReactJS? In ReactJS, components are the building blocks of your user interface. Components allow you to create modular, reusable UI elements, but sometimes these components need to communicate with each other.In this article, we will explore the different methods to pass data between components in ReactJS.1.
5 min read
How to build an HTML table using ReactJS from arrays ? To build an HTML table using ReactJS from arrays we can use the array methods to iterate to iterate the elements and create the table rows Prerequisites:NPM & Node.jsReact JSJavaScript Array.map()HTML TableApproach: To build an HTML table from an array of elements using ReactJS, we can use the a
2 min read