Home > other >  how to update previewState of string and keep last value
how to update previewState of string and keep last value

Time:04-05

i have state of string :

  const [selectQuery, setSelectQuery] = useState("lastValue");

how can i update the state with some value when onChange and keep the last state("lastValue")

here is what i tries with no success:

onChange={(e) => setSelectQuery((prevState) => prevState   e.target.value  )

it is not working good...

CodePudding user response:

Like this

const [state, setState] = useState("");

let onChange = (e) => {
  let oldValue = state; 
  let newValue = e.target.value;
  setState(newValue)
};

<Input type="text" value={state} onChange={onChange}></Input>;

CodePudding user response:

useEffect(()=>{
...

}, [selectQuery])

Did you try useEffect?

If you just want to update when selectQuery is triggered that's probably it.

  • Related