简体   繁体   English

在另一个组件中导入此组件时,如何使用 className 修改按钮组件

[英]How do I modify a button component with className when I importing this component in another component

I'm trying to modify a button depending on every page where is imported.我正在尝试根据导入的每个页面修改一个按钮。 Let's take a look at my button component.让我们来看看我的按钮组件。

 import React from "react"; import "./Button.css"; export interface Props { marginLeft?: string; name?: string; backgroundColor?: string; color?: string; width?: string; onClick?: () => void; border?: string; fontWeight?: number; fontSize?: string; lineHeight?: string; marginBottom?: string; height?: string; display?: string; alignItems?: string; } export const Button: React.FC<Props> = ({ name, backgroundColor, onClick, color, border, width, height, marginLeft, fontWeight, fontSize, lineHeight, marginBottom, display, alignItems, }) => { return ( <button className="myButton" style={{ background: backgroundColor, color: color, border: border, width: width, marginLeft: marginLeft, fontWeight: fontWeight, fontSize: fontSize, lineHeight: lineHeight, marginBottom: marginBottom, height: height, alignItems: alignItems, display: display, }} onClick={onClick} > {name} </button> ); }; export default Button;

The problem is coming from my mentor.问题来自我的导师。 He said that I should not do this like sending through props my css... and he suggested me to change it and to use a className on every specific place where I import my button and I wanted to change something.他说我不应该像通过 props 发送我的 css 那样这样做……他建议我更改它并在我导入按钮的每个特定位置使用 className 并且我想更改某些内容。

Here is how I did it until he suggested me to change it.在他建议我更改之前,我是这样做的。

 <Button backgroundColor="white" border="1px solid #595959" width="100%" name="Add a team member + " color="#595959" fontWeight={200} height="52px" marginBottom="25px" /> <Button width="13%" name="Submit Project" border="none" color="#FFFFFF" />

and I don't know exactly how I can write className on a component.而且我不知道如何在组件上编写 className。 I mean I get every time when I'm trying to do something a error that is suggesting me it can not be possible to do it.我的意思是每次当我尝试做某事时都会出现一个错误,提示我不可能做到这一点。 Can you guide me on how I should do it?你能指导我应该怎么做吗? I would asked my mentor if he was not that scary ... he doesn't smile that much ... you know he's a programming for over 8 yaers.我会问我的导师他是不是那么可怕……他没有那么多笑容……你知道他是一个超过 8 年的编程人员。

You could just create a css class for every variation of the button you want, and then write your button component like this:你可以为你想要的按钮的每个变体创建一个 css 类,然后像这样编写你的按钮组件:

const Button = ({class, name}) => {
  return (
    <button className={class}>{name}<button/>
  )
}

and when you want to use the Button当你想使用按钮时

<Button class='class1' name="Button with class 1"/>

We are creating button like common components to reuse the component and reduce the code repetition, if you are passing all the style values and props from all parent components(where Button is used) then there is no advantage of the common component or code reuse because you are duplicating everything in all the parent components.我们正在创建像公共组件一样的按钮来重用组件并减少代码重复,如果您从所有父组件(使用 Button 的地方)传递所有样式值和道具,那么公共组件或代码重用就没有优势,因为您正在复制所有父组件中的所有内容。 what should I suggest is create some CSS classes for similar style buttons, Let say you have 3 types of buttons我应该建议为类似样式的按钮创建一些 CSS 类,假设您有 3 种类型的按钮

  1. primary (will have a blue background and fixed sizes)主要(将具有蓝色背景和固定大小)
  2. secondary (Will have a grey background and fixed sizes)次要(将有灰色背景和固定大小)
  3. danger (Will have a red background and fixed sizes)危险(将有红色背景和固定大小)

So for these cases create 3 CSS in your CSS file所以对于这些情况,在你的 CSS 文件中创建 3 个 CSS

.btn {
  //common styles for button
   width="100px"
   fontWeight={200}
   height="52px"
   marginBottom="25px"
}
.primary {
   backgroundColor="blue"
   border="1px solid #595959"
   color="white"
}
.secondary {
   backgroundColor="grey"
   color="red"
}
.danger {
   backgroundColor="red"
   color="green"
}

after this change your button component like在此更改您的按钮组件后

import React from "react";
import "./Button.css";

export interface Props {
  name?: string;
  onClick?: () => void;
  className?:string;
}

export const Button: React.FC<Props> = ({
  name,
  onClick,
  className
}) => {
  return (
    <button
      className={`btn ${className}`}
      onClick={onClick}
    >
      {name}
    </button>
  );
};
export default Button;

And from your any parent component并从您的任何父组件

     <Button
          className="primary"
          name="Add a team member + "
        />
        <Button
          className="secondary"
          name="Submit Project"
        />

Note: This is just a sample code and classNames, you can use the class names and styles as you needed注意:这只是一个示例代码和类名,您可以根据需要使用类名和样式

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

相关问题 将样式导入到React组件中时,如何分配基于关闭状态进行更新的className? - When importing styles into a React component, how do I assign a className that updates based off state? 如何在反应功能组件中为按钮变量切换 className? - How do I toggle className for button variable in react functional component? 当我单击另一个组件上的按钮时如何更改组件状态 - How to change a component state when i click on a button that is on another component 如何将 function 分配给 React 中另一个组件的按钮? - How do I assign a function to a button from another component in React? 如何使用另一个文件中的按钮组件? - How do I use a button component from another file? 如何通过单击组件外部的按钮来刷新 React 组件? - How do I refresh a react component by clicking on a button outside the component? React.js如何在另一个组件中使用一个组件? - React.js How do I use a component in another component? 如何将组件引用传递给另一个组件? - How do I pass a component reference to another component? 如何在 Angular 中将数据从一个组件发送到另一个组件? - How do I send data from a Component to another Component in Angular? 在Ember中,如何创建对另一个组件的操作做出反应的组件? - In Ember, how do I create a component that reacts to the action of another component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM