Home > front end >  Input tag value update after onChange but want to update it instantly
Input tag value update after onChange but want to update it instantly

Time:03-25

I am working on a input box where when useState detailsData is true (which is on a radio button) then setName will be my name otherwise it will be user input , but problem is if it is true then it shows nothing unless i click on input and try to type a word and after that it shows string 'tejendra'.

 <input
                  type="text"
                  className="form_control"
                  value={name}
                  onChange={(e) => {
                    if (DetailsData === true) {
                      // setName(() => userDetailsData[1]?.name);
                      setName("tejendra");
                    } else {
                    setName(e.target.value);
                    }
                  }}
                />

CodePudding user response:

You should use useEffect to trigger a change in something else

useEffect(() => {
    if (DetailsData) {
        setName("tejendra");
    }
  }, [detailsData]);
  • Related