How to Pass JSON Values into React Components ? - GeeksforGeeks (2024)

Last Updated : 18 Apr, 2024

Summarize

Comments

Improve

In React, you can pass JSON values into components using props. Props allow us to send data from a parent component to its child components. When rendering a component, you can pass the JSON values as props. These props can then be accessed within the component’s function or class, allowing you to display the desired data.

Table of Content

  • Passing JSON values as props
  • Passing Array JSON

Steps to Setup the Application:

Step 1:Create a reactJS application by using this command

npx create-react-app myapp

Step 2:Navigate to the project directory

cd myapp

Project Structure:

How to Pass JSON Values into React Components ? - GeeksforGeeks (1)

The updated dependencies inpackage.jsonfile will look like:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Passing JSON values as props

Passing JSON values as props in React involves sending structured data objects from parent components to child components. This approach enables efficient data transfer and facilitates dynamic rendering, and helps developers to leverage JSON data for dynamic content display, state management, and application functionality within React components.

Example: In this example, the Account component receives the user prop containing the JSON data, which it can then use to display the user’s name and ID.

CSS
/*Account.css*/.centered { display: flex; justify-content: center; flex-direction: column; align-items: center; height: 100vh;}.green-text { color: green;}
JavaScript
// App.jsimport React, { Component } from "react";import Account from "./Account"; class App extends Component { constructor(props) { super(props); this.state = { user: { id: 12, firstName: "Bishal", lastName: "Paul", }, }; } render() { return ( <div> <Account user={this.state.user} /> </div> ); }}export default App;
JavaScript
// Account.jsximport React from 'react'import './Account.css'const Account = (props) => { const { user } = props return ( <div className="centered"> <h1 className="green-text"> GeeksforGeeks </h1> <span> Name - {user.firstName} {user.lastName} </span> <br /> <span>ID - {user.id}</span> </div> )}export default Account

Output:

How to Pass JSON Values into React Components ? - GeeksforGeeks (2)

Passing Array JSON

Passing arrays in JSON allows you to send multiple values in a single variable. JSON is a lightweight data-interchange format that is not user friendly for humans to read and write, and easy for machines to parse and generate.

Example: Implementation to show passing array JSON form one component to another.

JavaScript
// App.jsimport React from 'react'import ChildComponent from './ChildComponent.jsx'const ParentComponent = () => { const data = [ { id: 1, name: 'Bishal' }, { id: 2, name: 'Baishali' }, { id: 3, name: 'Rahul' } ] return ( <div style={{ textAlign: 'center', color: 'green' }}> <h1>GeeksforGeeks</h1> <p>Parent Component</p> <ChildComponent data={data} /> </div> )}export default ParentComponent
JavaScript
// ChildComponent.jsimport React from 'react';const ChildComponent = ({ data }) => { const jsonData = JSON.stringify(data); return ( <div> <p>Child Component</p> <pre>{jsonData}</pre> </div> );};export default ChildComponent;

Output:

How to Pass JSON Values into React Components ? - GeeksforGeeks (3)

Conclusion:

In React, passing JSON values as props is a common way to send data from a parent component to its child components. Props allow components to be dynamic and reusable, as they can receive different JSON data based on the parent’s state or other factors. JSON arrays can also be passed as props, allowing for the transmission of multiple values in a single variable. JSON’s simplicity and readability make it an ideal format for transmitting complex data structures between components in a React application.



Please Login to comment...

How to Pass JSON Values into React Components ? - GeeksforGeeks (2024)
Top Articles
Who Actually Made Money From The Crash In The Big Short (& How Much)
Tarpaulin Sizes and Printing Tips | Limkaco Industries, Inc.
The Exorcist: Believer Showtimes Near Regal Waugh Chapel
Reli Stocktwits
Methodist Laborworkx
Mall At Millenia Nordstrom
Citymd West 104Th Urgent Care - Nyc Photos
Boat Trader Wisconsin
Study Restaurants Near Me
2024 Fantasy Basketball Rankings, News and Draft Kit
Walgreens On Osborn And Scottsdale
Rock Auto Parts Catalogue
Rescare Training Online
Cobblemon Resource Pack
Baby Alien Fan Bus Video: A Closer Look
24 Hour Drive Thru Car Wash Near Me
Quick Links for Qtrac Login
Ixl Ld Northeast
Duelies Sports Bar And Grill Photos
Chico Ca Craigslist
Craigslist General Labor Annapolis
Blairsville Online Yard Sale
Nrsv Oremus Bible Browser
Tutorial - PyMuPDF 1.24.10 documentation
Infinite Campus Farmingdale
Brittanya 187 Clothing
Rhian Sugden Forum
Jessica Ann Ussery Wiki
Ascension St John Tulsa Patient Portal
"If you wake us up again tomorrow we will bring all our amplification into a room and play at 3 o'clock in the morning": When Ritchie Blackmore had an "embarrassing" encounter with Eric Clapton
Understanding North Star Metrics | Planio
The Cure Average Setlist
Samsung Tu7000 Vs Q60B
26200 E 64Th Ave
268000 Yen To Usd
Craigslist Personals Palm Springs California
September full moon is a Super Harvest Moon September 17-18
Frankfort Busted Newspaper
Jamie Kagol Married
What is 9xflix com: Dual Audio Movies and Web Series to Download – Review of Guide and Alternatives in 2023-LDPlayer's Choice-LDPlayer
4084716729
Boone County Sheriff 700 Report
Anthem Bcbs Otc Catalog 2022
Casa Grande Az Craigslist
Kingpin Parking Reviews
Fgo Spirit Root
Netid.unm.edu
What Time Is First Light Tomorrow Morning
West Coast Budmail
Cookie Clicker The Advanced Method
Stranizza D'amuri Streaming Altadefinizione
Espn Masters Leaderboard
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 6042

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.