Subscribe Us

header ads

Contoh Program UseState di React

 Beikut adalah contoh penggunakan UseState di React, untuk mempercantik tampilan saya akan menyertakan App.css


Program App.js

import "./App.css";
import { useState } from "react";

function App() {
  const [count, setCount] = useState(0);

  const handleIncrement = () => {
    setCount((count) => count + 1);
  };

  const handleDecrement = () => {
    setCount((count) => count - 1);
  };

  return (
    <div>
      <div className="hook">UseState</div>
      <div className="App">
        <div className="counter-row">
          <button onClick={handleDecrement}>Decrement</button>
          <div className="counter-value">{count}</div>
          <button onClick={handleIncrement}>Increment</button>
        </div>
      </div>
    </div>
  );
}

export default App;


Program App.css

.App {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 25px;
  background: linear-gradient(135deg, #f8fafc 60%, #e0e7ff 100%);
  padding: 40px 30px;
  border-radius: 18px;
  box-shadow: 0 6px 24px rgba(78, 84, 200, 0.12);
  max-width: 350px;
  margin: 0 auto;
}

.App-logo {
  height: 40vmin;
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  .App-logo {
    animation: App-logo-spin infinite 20s linear;
  }
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.hook {
  font-size: 2.5rem;
  font-weight: bold;
  text-align: center;
  margin-top: 40px;
  margin-bottom: 30px;
  color: #4e54c8;
  letter-spacing: 2px;
  text-shadow: 1px 2px 8px #bfcfff;
}

.App button {
  min-width: 130px;
  font-size: 1.1rem;
  font-weight: 600;
  padding: 12px 0;
  border-radius: 8px;
  border: none;
  background: #4e54c8;
  color: #fff;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
  box-shadow: 0 2px 8px rgba(78, 84, 200, 0.08);
}

.App button:hover {
  background: #565bda;
  transform: translateY(-2px) scale(1.04);
}

.App div {
  font-size: 2.8rem;
  font-weight: 700;
  color: #22223b;
  margin: 10px 0;
  letter-spacing: 1px;
  text-shadow: 0 2px 8px #e0e7ff;
}

.counter-row {
  display: flex;
  align-items: center;
  gap: 24px;
  justify-content: center;
}

.counter-value {
  font-size: 2.5rem;
  font-weight: 700;
  color: #22223b;
  min-width: 60px;
  text-align: center;
}


Tampilan ketika program dijalankan



Posting Komentar

0 Komentar