Answers for "react hooks setstate prevstate"

2

prevstate in usestate

const [prevState, setState] = React.useState([]);

setState(prevState => [...prevState, 'somedata'] );
Posted by: Guest on June-09-2020
32

useRef

/*
	A common use case is to access a child imperatively: 
*/

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}
Posted by: Guest on March-29-2020
1

this.setstate prevstate

this.setState(prevState => ({ n: !prevState.n }))
Posted by: Guest on January-25-2021

Code answers related to "react hooks setstate prevstate"

Code answers related to "Javascript"

Browse Popular Code Answers by Language