N
N
NooBick2021-11-09 00:28:50
React
NooBick, 2021-11-09 00:28:50

How to pass two functions one by one to onClick()?

<PanelHeaderButton onClick={this.props.go} data-to='Home'><Icon28ChevronLeftOutline/></PanelHeaderButton>


The code above works fine, it navigates back to the Home tab. But I need to make sure that there are two functions in onClick. The main thing is that this.props.go receives data-to.

This is what it looks like

const [activePanel, setActivePanel] = useState('Home');

  const go = e => {
    setActivePanel(e.currentTarget.dataset.to);
  };

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Proskurin, 2021-11-09
@NooBick

Usually, an event handler is defined, and the necessary functions are called in it

const handleClick = useCallback((ev) => { // меморизирую, т.к. обработчик передается в компонент а не HTML элемент
    props.go(ev); // более правильно конечно передавать не объект события, а конкретное значение. Функция go не должна ничего знать про событие
    // тут вызов второй функции
}, []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question