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.