0

I want to implement a color option in my button components to use it in all my global components so I can change the text and color Dynamically

is it correct?

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

const Button = ({text, BtnVersion1}) => {


    return (
        <a href="#" style={{color: {BtnVersion1}}} className="buy">{text}</a>
    )
}

export default Button;


<Button style={{color: "red"}} text="SHOP NOW" />

5 Answers 5

1
Your component code seems to be fully correct. But when using your component you have to pass props this way:
<Button text={"SHOP NOW"} BtnVersion1={"red"} />
Sign up to request clarification or add additional context in comments.

Comments

1

This is where styled components can be very useful. Where you're declaring 'style' on the component, that is an inline style and would only exist on that instance of the button. You can pass a prop, say 'btnColor' to the button which is used by the styled component to change the color. See below.

import React from 'react';
import styled from 'styled-component';
import './Button.css';

const StyledButton = styled.a`
   background-color: ${props => props.btnColor ? props.btnColor : 'red'};
`

const Button = ({
   text, 
   btnColor
}) => {
    return (
        <StyledButton href="#" btnColor={btnColor} className="buy">{text}</StyledButton>
    )
}

export default Button;


<Button btnColor='red' text="SHOP NOW" />

You can see above in the StyledButton that if we provide the btnColor prop than use that color, else default to 'red.'

For more information on styled components - https://styled-components.com/

Comments

1

You can have a more "dynamic" way of doing it (so your instance of <Button> doesn't change):

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

const Button = ({ text, style }) => {

    return (
        <a href="#" style={style} className="buy">{text}</a>
    )
}

export default Button;


<Button style={{ color: "red" }} text="SHOP NOW" />

If you want to just control the color (or other properties) you can do something like:

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

const Button = ({ text, color }) => {

    return (
        <a href="#" style={{ color }} className="buy">{text}</a>
    )
}

export default Button;


<Button color="red" text="SHOP NOW" />

Comments

1

You might want to use your component this way: The advantage here is that you can pass all your custom styles for every single one of your buttons.

const Button = ({text, style}) => {
  return (
     <a href="#" style={style} className="buy">{text}</a>
  );
};

ReactDOM.render(<Button style={{color: "red"}} text="SHOP NOW" />, document.querySelector("#root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>

<div id="root">
  <!-- Your root container -->
</div>

Comments

1

Donot use anchor tag as a button in react , If you want to know the reason read this doc https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md

I think best way to style a button is using styled component , I have given an example code in this sandbox , you can pass props for things only you want to change like for background, if no value is given it will take default color

try it

Edit dank-cloud-lsvvr

code :

import "./styles.css";
import styled from "styled-components";

const Button = styled.button`
  font-family: "Work Sans", sans-serif;
  font-size: 14px;
  font-weight: 600;
  color: #ffffff;
  line-height: 16px;
  background: ${(props) => (props.background ? props.background : "#00D2AD")};
  transition: all 0.4s ease-out;
  transition: all 0.4s ease-out;
  padding: 14px 24px;
  outline: none;
  text-align: center;
  display: inline-block;
  border: none;
  cursor: pointer;
  border-radius: 4px;
`;


export default function App() {
  return (
    <div className="App">
      <Button>Default Button </Button>
      <br />
      <br />
      <Button background={"gray"}>Custom Button</Button>
    </div>
  );
}


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.