1

I have created a button component in react. Functionality is working but I'm unable to change the button colors and style

parent.js

import React, { Component } from "react";
import {Button, TYPES} from '../../../Global/button';

class ProductDisplay extends Component{
  render(){
    return(
      <Button buttonType={TYPES.DANGER} label="Add To Cart" onClick={() => 
        this.addItemToCart(pdpList.uniqueID)}></Button>
    )
  }
}

export default ProductDisplay;

button.js

import React from 'react';
import classnames from 'classnames';
export const TYPES = {
  PRIMARY: 'btn-primary',
  WARNING: 'btn-warning',
  DANGER: 'btn-danger',
  SUCCESS: 'btn-success',
}

export const Button = (props) => (
  <button buttonType={props.TYPES} onClick={props.onClick} classnames={ 
   [props.buttonType || TYPES.PRIMARY] 
  }>
    {props.label} 
  </button>
);

I'm not able to change the color. Can anybody help me

0

2 Answers 2

2

instead of buttonType you need to use props here

 [props.buttonType || TYPES.PRIMARY]

You are already passing the buttonType from the parent component.

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

but my color is not changing
1

In button.js:

export const Button = (props) => (
  <button
    onClick={props.onClick}
    className={styles[props.buttonType || TYPES.PRIMARY]} 
  >
    {props.label} 
  </button>
);

Bear in mind that the prop name for classes is singular and camelcase, i.e. className rather than classnames.

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.