Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usestate with object

import React, { useState } from "react";

function Form() {
  const [user, setUser] = useState({
    name: "",
    email: "",
    password: "",
  });
  const { name, email, password } = user;
  const handleChange = (e) => {
    const { name, value } = e.target;
    setUser({
      ...user,
      [name]: value,
    });
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    console.log("Form Submitted");

    console.log(user);
  };

  return (
    <div>
      <h1>Registraction</h1>
      <form action="" onSubmit={handleSubmit}>
        <div className="formGroup">
          <label htmlFor="name">Name: </label>
          <input
            type="text"
            name="name"
            id="name"
            value={name}
            onChange={handleChange}
            required
          />
        </div>
        <div className="formGroup">
          <label htmlFor="email">Email: </label>
          <input
            type="email"
            name="email"
            id="email"
            value={email}
            onChange={handleChange}
            required
          />
        </div>
        <div className="formGroup">
          <label htmlFor="password">Password: </label>
          <input
            type="password"
            name="password"
            id="password"
            value={password}
            onChange={handleChange}
            required
          />
        </div>
        <div className="formGroup">
          <button className="count_btn" type="Submit">
            Register
          </button>
        </div>
      </form>
    </div>
  );
}
export default Form;
Comment

javascript react useState update object

- Through Input

        const [state, setState] = useState({ fName: "", lName: "" });
        const handleChange = e => {
            const { name, value } = e.target;
            setState(prevState => ({
                ...prevState,
                [name]: value
            }));
        };

        <input
            value={state.fName}
            type="text"
            onChange={handleChange}
            name="fName"
        />
        <input
            value={state.lName}
            type="text"
            onChange={handleChange}
            name="lName"
        />
   ***************************

 - Through onSubmit or button click
    
        setState(prevState => ({
            ...prevState,
            fName: 'your updated value here'
         }));
Comment

usestate with object

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

function App() {
  const [details, setDetails] = useState({ email: "", password: "" })
  const onChange = (e) => {
    const { name, value } = e.target;
    setDetails({ ...details, [name]: value })
  }
  const submitHandler = (e) => {
    e.preventDefault();
    alert(JSON.stringify(details))
  }
  return (
    <div className="App">
      <form onSubmit={submitHandler}>
        email : <input type="text" name="email" onChange={onChange} value={details.email} />
       //or <input type="text" name="email" onChange={(e) => setDetails({ ...details, email: e.target.value })} value={details.email} /><br /><br />
          password :<input type="text" name="password" onChange={onChange} value={details.password} />
        <button type="submit">Submit</button>
      </form>
      <h2>{details.email}</h2>
      <h2>{details.password}</h2>
    </div>
  );
}

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: date().toisostring().slice(0 10) giving wrong result 
Javascript :: slide right jquery 
Javascript :: node mailer office 365 
Javascript :: how to zip two arrays in javascript 
Javascript :: multer rename file 
Javascript :: get base url vuejs 
Javascript :: find word in javascript string 
Javascript :: how to add event listener to iframe 
Javascript :: jquery clone and append 
Javascript :: process memory usage javascript 
Javascript :: set cookie javascript 
Javascript :: array comprehension javascript 
Javascript :: javascript hex to string 
Javascript :: common character count javascript 
Javascript :: javascript function description standards 
Javascript :: scroll to bottom react 
Javascript :: ajax run function after page load 
Javascript :: javascript sort by id 
Javascript :: javascript date to string 
Javascript :: mouseevent clientx javascript 
Javascript :: vanilla javascript remove data attribute 
Javascript :: add expiry to jwt extended token 
Javascript :: javascript change border radius 
Javascript :: javascript get element by class name change style 
Javascript :: Codewars Convert a String to a Number! 
Javascript :: regex not ending with 
Javascript :: parsefloat.tofixed in javascript 
Javascript :: debounce in react native hooks 
Javascript :: array differenc javascript 
Javascript :: vscode file cannot be loaded because running scripts is disabled on this system 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =