DEV Community

Cover image for toggle button with react
Simone Gentili
Simone Gentili

Posted on

toggle button with react


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.


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


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


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

Top comments (0)