Day 6: Dynamic React Components using Props

React utilizes a component-based architecture that enables developers to create reusable and modular UI elements. A fundamental concept in React, props, plays a crucial role in managing and passing data between components.

Working with Props in React

Props, short for properties, pass data from a parent component to its child components. Props are read-only and cannot be modified directly by the receiving component. Props provide a way to make components dynamic and reusable. To pass props to a component, we pass them as attributes when rendering the component. This allows us to configure the behaviour and appearance of child components based on the data provided by the parent component.

export const Productcard = (props) => {
  const { image,  title, price} = props.data;
  return (
    <div>
      <h1>{title}</h1>
      <p>{description}</p>
    </div>
  );
};        

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format for storing and exchanging data. It consists of key-value pairs and is widely supported by different programming languages and APIs.

We can work with JSON data that needs to be passed into components for rendering dynamic content. This data can come from third-party APIs or be read from external files. In our case, we entered some data manually in a JSON file. Passing JSON values into React components allows us to create flexible and reusable UI components that can be dynamically populated with data.

Once the JSON file is ready, we import it to our react application.

Import ProductsData from ‘./../product-data.json’        

After importing the JSON data, we can access its values like any other JavaScript object. JSON data is typically structured as nested objects or arrays. To access specific values, we can traverse the JSON structure using dot notation or array indexing.

Traversing JSON

Traversing the JSON structure using array indexing refers to accessing and navigating through the elements of a JSON object or array by specifying the index position of the desired element.

export const DisplayProducts=()=>{

    return(
        <>
            <div className="container-fluid">
            <div className="row row-cols-1 row-cols-md-4 g-4">
                {ProductsData.map((product) =>(
                    <Productcard 
                    key={product.id}
                    image={product.product_image_url}
                    title={product.product_title}
                    price={product.product_price}
                    />
                ))}
            </div>
            </div>
        </>
    )
}        

Thanking our mentor Bikash Karki for today's lesson, I conclude this article.


To view or add a comment, sign in

More articles by Aastha S.

  • ExpressJS

    What is ExpressJS? Express.js is a Node.

  • Day 13: Forms in React (Formik and Yup)

    On the thirteenth day, I learned about forms in React, the Fragment tag, and react-icons in the react application…

  • Day 12: localStorage

    It can be infuriating to close a webpage while filling out a form accidentally. We lose all the data already filled and…

  • Day 11: Layout, Offset, and the useParams Hook

    Layout and Offset In React, "offset" and "limit" are commonly used when dealing with paginated data. These terms are…

  • Day 10: Using API data and Pagination in React

    APIs in React APIs (Application Programming Interfaces), in React, are used to fetch data from external sources, such…

  • Day 9: Environment Variables and Toastify in React

    What are Environment Variables? Environment variables in React serve as dynamic configurations for our application…

  • Day 8:React Hooks (Part 2)

    After a thorough introduction to React Hooks and the useState hook on day 7, we learned about the useEffect hook in…

  • Day 7: React Hooks

    On the seventh day of learning React, we learned about React hooks, their use, and their applications. React Components…

  • Day 5: React Layouts, Outlets, and Links

    On the fifth day of learning react, we grabbed on concepts like layouts, outlets, and links in React. They are…

  • Day 4: React-Router-DOM and BootStrap

    On the fourth day of learning React, we learned about installing Bootstrap and React router DOM, all thanks to our…

Explore content categories