0
function Profile() {


    const [userId, setUserId] = useState(localStorage.getItem("user_id"))
    const [fname, setFname] = useState()
    const instance = axios.create({baseURL: server});

    useEffect( ()=> {
      
      const getUserValues = async () => {

        const response = await instance.get(
          `Users?id=${userId}`

        )
          setFname(response.data[0].fname)
          console.log(response.data[0].lname)
      }
      getUserValues();
    },[])



    const editValues = {
      firstname: fname,
    };

    const [editUserValue, setEditValue] = useState(editValues);


    console.log(fname) return( <input className="form-control" type="text" name="firstname" value={editUserValue.firstname} )

I'm having issues on getting my editvalues to display in the input because of the rendering that react has on usestate. In the console.log(fname) i get first a undefined then another console log of the value that is needed. But i cant get that value to display on the input.

The updated value should be taken by editvalues, but editvalues only takes the non updated data.

Any suggestions?

teaticdev
  • 1
  • 2
  • 1
    The `useEffect` hook runs after the render, so initially you are logging `fname` before the `useEffect` has a chance to run. – sma Oct 27 '21 at 14:46

0 Answers0