6

I have a button component that I wish to re-use in reactJs, I want multiple of the same component to be rendered but using different styles and to use a different style when the button is pressed and also mouse HoverOver

The button component is: -

import React, { Component } from 'react';
//import './Buttons.css'
const buttons = (props) => {



    return (
      <div className="">

        <button id="buttons" class="-"> {props.name}</button>

        </div>

    );
  }


export default buttons;

The main App component holds a state which then the state displays the name of the Button, which I am sure there is a better way of doing this.

Also I have made multiple of a button component and I can apply css styles that way but I am sure this is not the correct way.

class App extends Component {



  state = {
    name: [
      { button: 'Login' },
      { buttonTwo: 'Sign Up' },
      { buttons: 'Login' }
    ],

  }
  render() {

    const style = {
      backgroundColor: 'blue',
      font: 'inherit',
      border: '1px solid red',
      padding: '8px',

    };



    var pressed = true
    function toggle() {
      pressed = !pressed

      //When pressed Styles change
    }

    return (
      <div className="App">

       {/* <Right />  Temp Disabled*/}
        <span><Button pressed={pressed} defaultPressed={true} pressedStyle={{color: 'blue'}}
        name={this.state.name[0].button} /></span>
        <span><Button name={this.state.name[1].buttonTwo} /></span>

      </div>
    );
  }
}

export default App;

I eventually aim to create a Log in section so these buttons will be part of that.

1
  • Pass the class as a props to the component Commented May 9, 2018 at 16:54

1 Answer 1

1

Why not to use just CSS and handle your "mouse HoverOver" and "button is pressed" with native :active and :hover pseudo-classes? If your styles will be in the CSS, then you can just pass some string as a parameter (e. g. className) and simply define necessary styles in the .css file

For example

class Application extends React.Component {
  render() {
    return (
      <div className="application-wrapper">
        <Button className="red">This is the red button</Button>
        <Button className="blue">This is the blue button</Button>
      </div>
    )
  }
}

class Button extends React.Component {
  render() {
    return (
      <button className={`my-awesome-button ${this.props.className}`}>{this.props.children}</button>
    )
  }
}

ReactDOM.render(<Application />, document.getElementById("kappa"))
.my-awesome-button {
  line-height: 40px;
  padding: 0 20px;
  border: none;
  background: black;
  color: white;
  font-weight: bold;
  cursor: pointer;
  outline: none;
}

.my-awesome-button:hover {
  opacity: .54
}

.my-awesome-button:active {
  opacity: .38
}

.my-awesome-button.red {
  background: red
}

.my-awesome-button.blue {
  background: blue
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="kappa"></div>

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

Comments

Your Answer

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