DEV Community

Sowntharya
Sowntharya

Posted on Edited on

ReactJS

1. Program for registration form with five fields:
Name
Email
Phone
City
Password

Need to perform three tasks:
Allow the user to enter information.
Store the information in React state.
When the user clicks Submit, display the entered information.

Concepts used in the program.

1.JavaScript import/export- To use and share components
2.React components- To create UI
3.Functions - To execute logic
4.Objects - To store multiple values
5.useState - To store changing data
6.Event handling - To handle user actions
7.Event object - To get input values
8.Spread operator - To update objects
9.JSX - To write HTML-like UI
10.Conditional rendering - To display data after submission
11.Re-rendering - To understand how React updates UI

import React, { useState } from 'react'

function FormPage() {

    //(name, email, phone, city, password)
    const [user, setUser] = useState({

    name : "",
    email: "",
    phone: "",
    city: "",
    password: ""

});

const [submitted, setSubmitted] = useState(false);

function getData(e) {
    e.preventDefault();  //prevent data
    setSubmitted(true);
    console.log("Submitted Data:", user);
}

function handleInput(e){
    let key = e.target.name;
    console.log(key);

    setUser({...user, [key]: e.target.value })
    console.log(user); 

   //console.log("=======");
}

    return(
        <div>
            <form onSubmit= {getData}>
                <label htmlFor= "name">Name</label>
                <input id='name' name='name' type="text" onInput={handleInput}/> <br   /><br   />

                <label htmlFor= "email">Email</label>
                <input id='email' name='email' type="email" onInput={handleInput}/> <br   /><br   />

                <label htmlFor= "phone">Phone</label>
                <input id='phone' name='phone' type="text" onInput={handleInput}/>  <br   /><br   />

                <label htmlFor= "city">City</label>
                <input id='city' name='city' type="text" onInput={handleInput}/>    <br   /><br   />

                <label htmlFor= "password">Password</label>
                <input id='password' name='password' type="text" onInput={handleInput}/>   <br   /><br   />

                <button type='submit'>Submit</button>

            </form>

            {submitted && (           //conditional rendering
                <div>
                    <h2>Submitted Details</h2>

                    <p>Name: {user.name}</p>
                    <p>Email: {user.email}</p>
                    <p>Phone: {user.phone}</p>
                    <p>City: {user.city}</p>
                    <p>Password: [Hidden]</p>
                </div>
            )}
        </div>
    ) 
}

 export default FormPage

Enter fullscreen mode Exit fullscreen mode

App.jsx

import FormPage from './FormPage';

function App() {
  return (
    <div>
      <FormPage />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

2. Checkbox Selection List
User can select multiple skills:
☐ React
☐ Node
☐ Java
☐ SQL

import React, { useState } from 'react'

function CheckBoxPage() {

    const [skils, setSkils] = useState([]);

function getData(e) {

    e.preventDefault();
    console.log(skils);

    }

function handleInput(e) {
        // console.log(e.target.name);
        // setSkils([...skils, e.target.name]);

    console.log(e.target.checked); 

    if (e.target.checked) {
        setSkils([...skils, e.target.name]);
    } 
    else {
        setSkils(skils.filter((skil) => e.target.name != skil)) 
    }   

    }

    return (
        <div>
            <form onSubmit={getData}>
                <label htmlFor="React">React </label>
                <input id='React' name='React' type="checkbox" onChange={handleInput} /> <br /><br />

                <label htmlFor="Node">Node </label>
                <input id='Node' name='Node' type="checkbox" onChange={handleInput} /><br /><br />

                <label htmlFor="Java">Java </label>
                <input id='Java' name='Java' type="checkbox" onChange={handleInput} /><br /><br />

                <label htmlFor="SQL">SQL </label>
                <input id='SQL' name='SQL' type="checkbox" onChange={handleInput} /><br /><br />

                <button type='submit'>Submit</button>
            </form>
        </div>
    )
}

export default CheckBoxPage

Enter fullscreen mode Exit fullscreen mode

App.jsx file:

import CheckBoxPage from './CheckBoxPage';

function App() {
  return (
    <div>
      <CheckBoxPage />
    </div>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)