0

all i want is, after changing the state, i want to run the second argument..

without hooks.. this is what it looks like

state = {
  gasTypeFrom: '',
}

setModal = item => {
    setState({ gasType: item }, () => {
      renderFrom();
    });
  };

this is what i tried with hooks

 const [froms, setFroms] = useState({
    gasType: 'Select Value Here',
    displayModal: false,
  });

function setModalFrom(item) {
    useEffect(
      () => {
        setFroms({...froms, gasType: item});
      },
      () => {
        renderModalFrom();
      }
    );
    console.log('setModalFrom()', froms.gasType);
  }

how do i do it in hooks with a second argument?

Кріс
  • 147
  • 1
  • 2
  • 7
  • Possible duplicate of [React hook equivalent to callback function after setting state](https://stackoverflow.com/questions/54895801/react-hook-equivalent-to-callback-function-after-setting-state) – Snoopy Sep 11 '19 at 07:00

1 Answers1

1

useEffect takes a function callback and a dependency array, so when a value in the dependency array is updated the effect is fired.

const [froms, setFroms] = useState({
  gasType: 'Select Value Here',
  displayModal: false,
});

useEffect(() => {
  renderModalFrom();
}, [froms]); // when a value here updates, effect is run

...somewhere in your code
setFroms({...froms, gasType: item}); // this updates the value

useEffect documentation

Drew Reese
  • 165,259
  • 14
  • 153
  • 181