1

I can't make react onChange to fire on the first keystroke, the second input works. My goal is to get the new fName value to be used in other functions.

export default function Name() {
  const [fName, setfName] = React.useState("");

  return (
      <input
        type="text"
        placeholder="First Name"
        onChange={(e) => {
          setfName(e.target.value);
          console.log("fname", fName);

          someOtherFunctions(fName); // i need to keep the function here to react to each user input change. how to get the right fName value here? 
        }}
      />

}

Typing 'abcd' and console prints:

fname 
fname a
fname ab
fname abc
10
  • 1
    try console.log(e.target.value) Commented Mar 14, 2022 at 4:28
  • @zainuldin im in new to react useEffect, not sure how to code it. can you give an example (i updated my code too) Commented Mar 14, 2022 at 4:53
  • pass e.target.value to function that's it Commented Mar 14, 2022 at 5:00
  • @zainuldin if that's the thing, what's the point of setfName Commented Mar 14, 2022 at 5:04
  • you can't update you're value in UI with out using useState Commented Mar 14, 2022 at 5:06

4 Answers 4

1

try this approach do console.log before of return whenever state will change it will re-render the page. Have a look at this code

import "./styles.css";
import React , {useState , useEffect} from 'react'


export default function App() {
  const [fName, setfName] = useState("");

 // every time state change react will automatically re-render the page
// console.log(fName)
 
function PrintMyState(state){
   console.log(state)
}
 
// calls whenever state will be change , another approach
useEffect( ()=>{ PrintMyState(fName) } , [fName] ) 

 return (

  <input
    type="text"
    placeholder="First Name"
    onChange={(e) => {
     setfName(e.target.value)
    }}
   />
 );
}

sand Box Demo click here

Sign up to request clarification or add additional context in comments.

5 Comments

it is not what i was trying to solve .. thx anyway
what you are trying to slove please explain
your question was react useState is not taking 1st input but now it's working
try to get the right fName value inside the onChange function. (see my updated code comment)
it's not possible i think react state take some time to update
0

Reactjs state is asynchronously updated, and batching within the function. So logging within the function itself the last state "" will be printed.

--> Try moving console.log("fname", fName); right before your return function.

Comments

-2

onChange function is fired in every keystroke. It looks like you misunderstand something about React Hooks. In your code, setfName is working asynchronously, so first console log prints the previous fName. console.log("fname", fName + e.target.value); will print current value correctly. or You can use useEffect hook to print current value.

2 Comments

i think i understand but not sure how to fix it, if my goal is to get the new fName value to be used in other functions.
@user3810193 you can pass it by props to other components
-2

you need to put value={fName} for the input to work:

<input
  type="text"
  value={fName}
  placeholder="First Name"
  onChange={(e) => {
      setfName(e.target.value);
      console.log("fname", e.target.value);
  }}
/>

for logging, run a useEffect call that runs everytime fName changes:

useEffect(() => {
  console.log(fName)
}, [fName])

the dependency array [fName] means that run the function inside useEffect, in this case console.log(fName) every time fName changes.

5 Comments

this is not the problem..
@user3810193 did you try it?
yes, still get the same console.log
@user3810193 nope, it should work. try making a codesandbox or something. see stackoverflow.com/a/58211012/6141587 or sebhastian.com/react-onchange
@user3810193 i misread your question. i thought input wasn't working. added an explanation for console.log to work using useEffect. since react state is asynchronous, you can't access it directly inside onChange call. you must run a useEffect to access it everytime it changes. see my code for explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.