简体   繁体   English

更改组件中 div 的类名属性

[英]Change classname attribute of a div in a component

I have a card-publication component and I want to be able to make the component reusable and when declaring the tag I can also declare its classname and the position to place it in.我有一个卡片发布组件,我希望能够使该组件可重用,并且在声明标签时,我还可以声明它的类名和 position 来放置它。

Component and main code.组件和主要代码。

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

and how should I declare them.以及我应该如何声明它们。

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

You can parsing the props from your declaration components to the component, there is an explain of your case.您可以将声明组件中的道具解析为组件,您的案例有解释。 You must set the arguments on your component first您必须首先在您的组件上设置 arguments

import React from 'react';

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

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

export default CardPublication

And then you can declaration your component with props and value that you want然后你可以用你想要的道具和价值来声明你的组件

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

So, the name props will parsing the argument to your component.因此,名称 props 会将参数解析为您的组件。 And you will have two div with "cards1" class and "cards2" class.您将拥有两个 div,其中“cards1”class 和“cards2”class。

You also can learn about components and props on https://reactjs.org/docs/components-and-props.html您还可以在https://reactjs.org/docs/components-and-props.html上了解组件和道具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM