惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
人人都是产品经理
人人都是产品经理
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
B
Blog
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
V
V2EX

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
UseState - Exercises
Sivakumar Ma · 2026-05-21 · via DEV Community

Sivakumar Mathiyalagan

*Toggle Button *

import { useState } from "react";

function Toggle(){

   const [bulb,setBulb] = useState("https://www.w3schools.com/js/pic_bulboff.gif")
//    const [flag,setFlag] = useState(false);





   function toggle(){
    if (bulb === "https://www.w3schools.com/js/pic_bulboff.gif"){
        setBulb("https://www.w3schools.com/js/pic_bulbon.gif")
        // setFlag(true);
    }
    else{
        setBulb("https://www.w3schools.com/js/pic_bulboff.gif")
        // setFlag(false);
    }
   }



    return(
        <>
        <img src= {bulb}></img>
        <button onClick = {toggle}>{bulb==="https://www.w3schools.com/js/pic_bulboff.gif"?"ON":"OFF"}</button>
        </>

    )


}

export default Toggle

Enter fullscreen mode Exit fullscreen mode

output:

Input Field Value

import { useState } from "react";

function Input(){

    const[name,setName] = useState('')




    return(
    <>
      <input value={name} type="text" onChange={(e)=>setName(e.target.value)}></input>
        <p>Hi! Welcome {name}</p>
        </>

    )

}

export default Input

Enter fullscreen mode Exit fullscreen mode

Output:

Show/Hide Text

function Show(){

    const[text,setText] = useState("");
    const[show,setShow] = useState(false);



    function showContent(){
        if(show===false){
             setShow(true);
        }
        else{
            setShow(false);
        }

    }

    return(
        <>
        <input type="password" onChange={(e)=>setText(e.target.value)}></input>
        <button onClick={showContent}>{show===false ?"Show":"Hide"}</button>
        {show && <p>{text}</p>}
        </>

    )
}

export default Show 

Enter fullscreen mode Exit fullscreen mode

Output:

Character Counter

import { useState } from "react";

function Character (){

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

    return(
        <>
        <input type="text" onChange={(e)=>setCount(e.target.value.length)}></input>
        <p>Count :{count} </p>
        </>

    )
}

export default Character

Enter fullscreen mode Exit fullscreen mode

Output:

Form with Multiple Input

import { useState } from "react";


function Multiple(){

    const[name,setName]= useState('');
    const[email,setEmail]= useState('');
    const[number,setNumber]= useState('');
    const[city,setCity]= useState('');
    const[password,setPassword]= useState('');



    return(
        <>
        <input type="text" onChange={(e)=>setName(e.target.value)}></input>
        <input type="email" onChange={(e)=>setEmail(e.target.value)}></input>
        <input type="text" onChange={(e)=>setNumber(e.target.value)}></input>
        <input type="text" onChange={(e)=>setCity(e.target.value)}></input>
        <input type="password" onChange={(e)=>setPassword(e.target.value)}></input>
        </>
    )
}

export default Multiple

Enter fullscreen mode Exit fullscreen mode

CheckBox Selection List

import { useState } from "react";

function Selection(){

    const [value,setValue] = useState([]);
    const [checked,setChecked] = useState(false);

   function addValue(e) {

        const checked = e.target.checked;
        const item = e.target.value;

        if (checked) {
            setValue([...value, item]);
        } else {
            setValue(value.filter((val) => val !== item));
        }
    }

    return(
        <>
        <input value="React" type="checkbox" onChange={addValue}></input>
        <label>React</label>
        <input value="Node" type="checkbox" onChange={addValue}></input>
        <label>Node</label>
        <input value="Java" type="checkbox" onChange={addValue}></input>
        <label>Java</label>
        <input value="SQL" type="checkbox" onChange={addValue}></input>
        <label>SQL</label>

        <ul>
                {value.map((val, index) => (
                    <li key={index}>{val}</li>
                ))}
            </ul>
        </>
    )
}

export default Selection

Enter fullscreen mode Exit fullscreen mode

Output:

Dependent Dropdown

import { useState } from "react";

function Dependent() {

    const states = {
        india: ["tamilnadu", "kerala"],
        usa: ["arizona", "newyork"]
    };

    const cities = {
        tamilnadu: ["chennai", "trichy"],
        kerala: ["trivandrum", "kochi"],
        arizona: ["phoenix"],
        newyork: ["washington"]
    };

    const [country, setCountry] = useState("");
    const [state, setState] = useState("");
    const [city, setCity] = useState("");

    return (
        <>

            <select value={country}
         onChange={(e) => {
        setCountry(e.target.value);
        setState("");
        setCity("");
        }}
        >   

                <option value="">Select Country</option>

                <option value="india">India</option>

                <option value="usa">USA</option>

            </select>


            <select value={state} onChange={(e) => {
                setState(e.target.value);
                setCity("");

            }

            }>

                <option value="">Select State</option>

                {
                    country &&
                    states[country].map((val, index) => (
                        <option key={index} value={val}>
                            {val}
                        </option>
                    ))
                }

            </select>


            <select value={city} onChange={(e) => setCity(e.target.value)}>

                <option value="">Select City</option>

                {
                   country && state &&
                    cities[state].map((val, index) => (
                        <option key={index} value={val}>
                            {val}
                        </option>
                    ))
                }

            </select>


        </>
    );
}

export default Dependent;

Enter fullscreen mode Exit fullscreen mode

Output: