content logo

React Buttons:

Day/Night Background Color Button using React

The button we put in this post is purple. This button has white text. The color of the screen is also dark gray. Clicking this button changes the text of the button and the color of the button changes to dark gray. The color of the screen also changes from dark gray to white. You can use this button to change the color theme of your website. This button is made using React.

#

HTML Background Color Button

#

Dark/LightL Button Code

#

React Button Color Change

#

Text Change React Button

<!-- This script got from frontendfreecode.com -->
<div id="app"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
body, html {
  height: 100%;
}
body body, html body {
  display: flex;
  margin: 0;
  align-items: center;
  justify-content: center;
  transition: 0.35s ease all;
  background: #1f1f1f;
}
.button {
  background: #666ed8;
  padding: 20px 25px;
  border: none;
  font-size: 22px;
  border-radius: 6px;
  cursor: pointer;
  color: white;
  font-weight: bold;
  transition: 0.3s ease all;
  box-shadow: 1px 2px 3px rgba(0, 0, 0, 0.3);
}
.button:focus {
  outline: none;
}
.button.clicked {
  background: #383737;
}
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}class App extends React.Component {constructor(...args) {super(...args);_defineProperty(this, "state",
    {
      clicked: false });_defineProperty(this, "toggleBtnHandler",
    () => {
      return this.setState({
        clicked: !this.state.clicked });
    });}
  render() {
    const styles = ['button'];
    let text = 'Click me!';
    let bodyEl = document.body;
    bodyEl.style.background = '#1f1f1f';
    if (this.state.clicked) {
      styles.push('clicked');
      text = 'Clicked!';
      bodyEl.style.background = '#EEE';
    }
    return /*#__PURE__*/(
      React.createElement("div", { className: app }, /*#__PURE__*/
      React.createElement("button", { className: styles.join(' '), onClick: this.toggleBtnHandler }, text)));
  }}
ReactDOM.render( /*#__PURE__*/React.createElement(App, null), document.getElementById('app'));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.8.2/umd/react.production.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.2/umd/react-dom.production.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.8.2/umd/react.production.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.2/umd/react-dom.production.min.js'></script>
<style>
body, html {
  height: 100%;
}
body body, html body {
  display: flex;
  margin: 0;
  align-items: center;
  justify-content: center;
  transition: 0.35s ease all;
  background: #1f1f1f;
}
.button {
  background: #666ed8;
  padding: 20px 25px;
  border: none;
  font-size: 22px;
  border-radius: 6px;
  cursor: pointer;
  color: white;
  font-weight: bold;
  transition: 0.3s ease all;
  box-shadow: 1px 2px 3px rgba(0, 0, 0, 0.3);
}
.button:focus {
  outline: none;
}
.button.clicked {
  background: #383737;
}
</style>

</head>
<body>
<div id="app"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}class App extends React.Component {constructor(...args) {super(...args);_defineProperty(this, "state",
    {
      clicked: false });_defineProperty(this, "toggleBtnHandler",
    () => {
      return this.setState({
        clicked: !this.state.clicked });
    });}
  render() {
    const styles = ['button'];
    let text = 'Click me!';
    let bodyEl = document.body;
    bodyEl.style.background = '#1f1f1f';
    if (this.state.clicked) {
      styles.push('clicked');
      text = 'Clicked!';
      bodyEl.style.background = '#EEE';
    }
    return /*#__PURE__*/(
      React.createElement("div", { className: app }, /*#__PURE__*/
      React.createElement("button", { className: styles.join(' '), onClick: this.toggleBtnHandler }, text)));
  }}
ReactDOM.render( /*#__PURE__*/React.createElement(App, null), document.getElementById('app'));
</script>

</body>
</html>
Preview