Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

state dependent on prev state in react js

import React, { useState } from "react";

const ExpForm = () => {
  // const [updateTittle, setUpdateTitle] = useState("");
  // const [updateNumber, setUpdateNumber] = useState("");
  // const [updateDate, setUpdateDate] = useState("");

  const [inputUPdate, setInputUpdate] = useState({
    updateTittle: "",
    updateNumber: "",
    updateDate: ""
  });

  const titleUpdateHander = (e) => {
    setInputUpdate((prevState) => {
      return {
        ...prevState,
        updateTittle: e.target.value
      };
    });
    console.log("value", inputUPdate.updateTittle);
  };
  const numUpdateHander = (e) => {
    setInputUpdate((prevState) => {
      return { ...prevState, updateNumber: e.target.value };
    });
    console.log("value", inputUPdate.updateNumber);
  };
  const dateUpdateHander = (e) => {
    setInputUpdate((prevState) => {
      return { ...prevState, updateDate: e.target.value };
    });
    console.log("value", inputUPdate.updateDate);
  };
  return (
    <form>
      <input
        value={inputUPdate.updateTittle}
        type="text"
        placeholder="Enter Exp Name"
        onChange={titleUpdateHander}
      />
      <input
        value={inputUPdate.updateNumber}
        type="number"
        min="0.1"
        step="0.1"
        onChange={numUpdateHander}
      />
      <input
        type="date"
        value={inputUPdate.updateDate}
        onChange={dateUpdateHander}
      />
      <input type="submit" value="Submit" />
    </form>
  );
};

export default ExpForm;
 
PREVIOUS NEXT
Tagged: #state #dependent #prev #state #react #js
ADD COMMENT
Topic
Name
5+1 =