简体   繁体   English

React Reusable Button Component - 如何正确传递背景颜色道具?

[英]React Reusable Button Component - How do I properly pass a background color prop?

Right now I am trying to make a reusable button component where I can pass in a color prop to set the background depending on what component it is in.现在我正在尝试制作一个可重用的按钮组件,我可以在其中传递一个颜色道具来根据它所在的组件设置背景。

CustomButton.js自定义按钮.js

const CustomButton = ({ children, link, color }) => {
  return (
    <a href={link}>
      <button className={styles.customButton} backgroundColor={color}>
        {children}
      </button>
    </a>
  );
};

App.js应用程序.js

<CustomButton link='/' color='red'>Test</CustomButon>

You should set the background color as part of the style property.您应该将背景颜色设置为样式属性的一部分。

const CustomButton = ({ children, link, color }) => {
  return (
    <a href={link}>
      <button 
         className={styles.customButton} 
         style={{background: color}}
      >
        {children}
      </button>
    </a>
  );
};

暂无
暂无

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

相关问题 在 react 如何使用可重用组件作为 React Component 传递到 react-router-dom 组件道具? - in react how to use reusable component as React Component to pass into react-router-dom Component prop? 如何在 React 中将 const 变量作为道具传递给组件? - How do I pass a const variable to a component as a prop in React? 如何在 React 和 Typescript 中将组件作为道具传递? - How do I pass a component as a prop in React and Typescript? 如何将 Prop 传递给导航屏幕组件 - React Native - How do I pass a Prop to a Navigation Screen Component - React Native 如何在父项更改时将道具传递给反应组件但不更新子项中的该道具? - How do I pass a prop to a react component yet not update that prop in the child when parent changes? 如果按钮是可重用的 React 组件,如何仅在一个选项中禁用 React 中的按钮? - How do I disable a button in React for just one option if the button is a reusable React component? 如何将React组件作为prop传递 - How to pass a React component as a prop 如何将附加组件作为道具添加到 React 中的可重用组件? - How to add additional component as prop to a reusable component in React? 如何将组件作为道具传递给 React 中的另一个组件? - How can I pass a component as a prop into another component in React? 当用户单击按钮时,如何保存可重复使用的React组件? - How do i make my reusable react component saves when the user clicks button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM