DEV Community

Cover image for toggle button with react
Simone Gentili
Simone Gentili

Posted on

3

toggle button with react

overview

I've created same post years ago in the same serie. Before I created 25 videos about react and some other about NextJs. Also, .. before meet vite. This is an updated version of an old article about toggle button creation with react.

requirements

You just need npm installed to run the following command



npm create vite@latest
cd app_name
npm install
npm run dev


Enter fullscreen mode Exit fullscreen mode

src/App.tsx

Replace src/App.tsx file with this:



import { useState } from 'react'
import './App.css'

function App() {
  const [pressed,setPressed] = useState(false)
  const label = pressed?'pressed':'unpressed'
  return (
    <>
      <h1>Toggle button</h1>
      <button onClick={() => setPressed(!pressed)}>{label}</button>
    </>
  )
}

export default App


Enter fullscreen mode Exit fullscreen mode

preview of toggle button

Conclusion

Study is better. Now React have hooks. Install with cdn is fine but create app with vite is cool.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay