I am getting error "Cannot read property 'fname' of undefined"

html dont have any code just importing this code

  import React, { useState } from "react";
  function App() {
  const [fullname, setFullName]= useState({
  fname: "",
  lname: ""
  });

  console.log(fullname.fname);


  function Name(event){
    const newvalue = event.target.value;
    const newname = event.target.name;

    setFullName(prevValue => {
      console.log(prevValue);
      
      if(newname === "fname"){
        return {
          fname: newvalue,
          lname: prevValue.lname          
        }
      }else if(newname === "lname"){
        return {
          fname: prevValue.fname,
          lname: newvalue
        }
      }
    })
  }
  console.log(fullname.fname);

  return (
    <div className="container">
      <h1>Hello </h1>
      <form>
        <input 
        onChange={Name}
        // value={fullname.fname}
        name="fName" 
        placeholder="First Name" />
        <input
        onChange={Name} 
        // value={fullname.lname}
        name="lName" 
        placeholder="Last Name" />
        <button>Submit</button>
      </form>
    </div>
  );
}
export default App;

When I am console.log fullname the it shows fname and lname but when i use it as fullname.fname or fullname.lname it sows error. I have defined fname and lname in useState what’s the problem I cant find it. I have tried console.log at multiple places it does not work

189 thoughts on “I am getting error "Cannot read property 'fname' of undefined"”

  1. Pingback: is stromectol
  2. Pingback: priligy hcl
  3. Pingback: uti stromectol 6mg
  4. Pingback: ivermect 125 mg

Leave a Comment