Update the class name property of a div within a component

I am working on creating a reusable card-publication component that allows me to specify the classname and position when declaring the tag.

Below is the code for the component:

import React from 'react';

function CardPublication({name}) {
    return (
        <div id={name}>
            <div id="Card Incard">

            </div>
            <p id="Descripcion"> Paragraph .</p>
        </div>
    );
}

export default CardPublication

Here is an example of how you can declare multiple instances of the component:

<CardPublication ></CardPublication>
<CardPublication ></CardPublication>

Answer №1

To pass props from your parent components to the child component, you need to define the props in the child component first. Here is an example of how you can do this:

import React from 'react';

function CardPublication({props}) {
    return (
        <div className={props.name}>
            <div id="Card Incard">

            </div>
            <p id="Description"> This is a paragraph.</p>
        </div>
    );
}

export default CardPublication

After defining the props in the child component, you can then use the component with different values for the props like this:

<CardPublication name="cards1"></CardPublication>
<CardPublication name="cards2"></CardPublication>

In this case, the "name" prop will be passed to the child component resulting in two divs with classes "cards1" and "cards2".

If you want to learn more about components and props in React, you can visit https://reactjs.org/docs/components-and-props.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rendering data from the server using backbone.js: A comprehensive guide

Initially, this is the response received from the server $response = array(); $i = 0; while($result = mysql_fetch_assoc($query)){ $response[$i]["id"] = $result["ID"]; $response[$i]["post_ ...

Issue encountered when attempting to use an HTML document as a blueprint for generating additional HTML documents with the

I am currently working on an exercise that involves creating four different files: index.html, home.html, about.html, and contact.html. The objective is to utilize jQuery's .load function to load the other three HTML files within index.html while stil ...

arranging three divs in a row, with one div expanding to fill the entire height while the other two divs evenly split the remaining

I am attempting to create a content row consisting of 4 divs: 1 container div 3 divs within the container: 1 image - taking up the full height 2 text - sharing the height, but each on a separate line. It should resemble the following: https://i.sstatic.n ...

Having trouble displaying an image in your React project?

I'm having an issue with my code where the logo is not displaying, even though it should be in the src -> pages -> images folder. I can't seem to figure out why this is happening. import dress from "./images/dress.png"; const I1 = () => ...

Having trouble processing a JSON object using Jquery?

Hello, I am currently working on parsing a JSON object using jQuery. So far, I have successfully extracted all the necessary information from the results except for one crucial piece - the performance tags. Each JSON result is enclosed in an event tag and ...

ThreeJS is giving an error message stating that it cannot find a corresponding function for 'mapTexelToLinear'

Currently grappling with a challenge in ThreeJS as I embark on a basic crash course. My hurdle lies within the Shader, specifically when attempting to texture a cube. Here's a snippet of my code where I create the cube: var geometry = new THREE.BoxGe ...

Exploring the world of chained JavaScript Promises for automatic pagination of an API

Dealing with a paged API that requires fetching each page of results automatically has led me to construct a recursive promise chain. Surprisingly, this approach actually gives me the desired output. As I've tried to wrap my head around it, I've ...

Creating a mechanism that fetches web links and automatically substitutes the URL with the corresponding title of the webpage

Just the other day, I noticed that putting a link in my profile resulted in it being replaced by the exact title of the question. This got me thinking, how exactly do they accomplish this? My hunch is that they achieve this by using ajax to send the link ...

Create a three-dimensional tree array in Typescript/Javascript by transforming a flat array

Received data is structured as follows: const worldMap = [ { "name": "Germany", "parentId": null, "type": "Country", "value": "country:unique:key:1234", "id&qu ...

Exploring the process of retrieving a pre-defined array within AngularJS

I'm encountering a challenge with accessing the array element in AngularJS. Here is the array: $scope.salesentry = [ { sales_id:'', product_id: '', product_category_id:'', sale_qty:nul ...

When using setTimeout in React, I encountered an error message saying that this.setState is not

In the current component, the state.breaker value is set to false. When the scroll event is detected, it checks the state, and if it's false, it performs certain actions. I am looking to introduce a static delay before the action can occur again. Tha ...

"Navigate back with just a click - XSL button

My expertise in this area is limited. After some searching, I couldn't find exactly what I need. Hopefully, the solution is simple. I am currently developing a software control system with a built-in web server. Certain points during navigation requi ...

Formatting code within an HTML document

I am attempting to customize a stock widget that I obtained from financialcontent.com. My current approach involves using Bootstrap for styling purposes. To incorporate the widget into a div, I found it necessary to embed the script directly within the HT ...

What is the method with the greatest specificity for applying styles: CSS or JS?

When writing code like the example below: document.querySelector('input[type=text]').addEventListener('focus', function() { document.querySelector('#deletebutton').style.display = 'none' }) input[type=text]:focu ...

Activate the element only when all input fields are completed. This function can be used repeatedly

I am working on developing a versatile function that will iterate through all input fields. If any of these fields are empty, I want to trigger the toggling of a class name (disabled) on another element (such as an anchor or button). Currently, the functi ...

Using dataloader on ammap effectively involves importing the necessary data into the platform

I've been attempting to implement the dataloader feature on ammap without success. Here is my current approach: var birth_map = AmCharts.makeChart( "il_bazinda_dogum_say_dagilim", { "type": "map", "data": { ...

How can I accurately show the server time and timezone in JS/jQuery regardless of the user's location?

Apologies for bringing up another time and timezone question related to web development. Despite numerous resources available, I am still struggling to grasp the concept of converting time between PHP and JavaScript. Here is my scenario: I aim to retrieve ...

Reduce the count by clicking a button while maintaining the previous state

export default class App extends Component { state = { info: [{ name: "Zuko", value: 1 }], }; handleClick = () => { this.setState((prev) => { console.log(prev.info[0].value); return { ... ...

Encountering a tabindex issue with React and Material-UI Modal functionality

I encountered an issue when trying to open a modal in my React application, but I am struggling to understand the error message and how to resolve it. "Warning: Material-UI: the modal content node does not accept focus. For the benefit of assistive techno ...

Having trouble with a malfunctioning JavaScript function? Need help resolving the issue?

My JavaScript code can be found at the following link: Link The function makewindows does not seem to be functioning properly. While it does create a window, the HTML content either shows as quoted text or if I use: child1.document.write(json_encode($ro ...