DEV Community

Bhavani Ravi
Bhavani Ravi

Posted on • Originally published at Medium on

2

ReactJS Buttons CheatSheet

Using buttons? You got them all here

Most frontend devs life revolves around creating components and styling them. Me as a backend dev, found myself going through 4–5 StackOverflow links before I could get a single button component up and running. Here is a list of basic operations.

Create New React Project

npx create-react-app react\_button\_cheatsheet

How to Create a Button


class UpgradedButton extends React.Component{
     render(){
          <button>Submit</button>
     }
}

Make It Look Pretty


const StyledButton = styled.button`
    position: absolute;
    height: 10%;
    width: 10%;
    top: 50%;
    left:50%;
    font-size: 2.6vmin;
    cursor: pointer;
    box-shadow: rgba(255, 255, 255, 0.05) 0px 3px 20px;
    border-width: initial;
    background-color: grey;
    color: white;
    border-style: none;
    border-color: initial;
    border-image: initial;
    outline: none;
`

**// change this line in UpgradedButton**  
// <button>Submit</button>
<StyledButton>Submit</StyledButton>

Change Color OnHover

Add this to the style components’ CSS string


const StyledButton = styled.button`
    ...
    ...
    ...
    &:hover {
       background-color: #445b65;
       color: white;
    }
`

Handle Onclick event


handleClick = () => {
    console.log("Button clicked...")
}

...
...

<Button onClick={this.handleClick}>Submit</Button>

Change Text OnClick

handleClick = () => {
   buttonText = this.state.buttonText == "Start" ? "Stop" : "Start"      
   this.setState({buttonText: buttonText})
}

The Complete Code

import React from 'react'
import styled, { css } from 'styled-components'
const Button = styled.button`
position: absolute;
height: 10%;
width: 10%;
top: 50%;
left:50%;
font-size: 2.6vmin;
cursor: pointer;
box-shadow: rgba(255, 255, 255, 0.05) 0px 3px 20px;
border-width: initial;
background-color: grey;
color: white;
border-style: none;
border-color: initial;
border-image: initial;
outline: none;
&:hover {
background-color: #445b65;
color: white;
}
`
export default class UpgradedButton extends React.Component{
constructor(props){
super(props)
this.state = {
buttonText: "Start"
}
}
handleClick = () => {
console.log("Button clicked...")
let buttonText = this.state.buttonText == "Start" ? "Stop" : "Start"
this.setState({buttonText: buttonText})
}
render(){
return (
<Button onClick={this.handleClick}>{this.state.buttonText}</Button>
)
}
}

Something is missing? Leave it on the comment. Liked what you read? Leave a shoutout on Twitter @geeky_bhavani

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay