DEV Community

Cover image for toggle button with react
Simone Gentili
Simone Gentili

Posted on

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.

Top comments (0)