X
X
xenonhammer2020-08-26 19:58:25
Yandex
xenonhammer, 2020-08-26 19:58:25

How to set goals for Yandex metrics on a button in react. Typescript swears?

There are targets for the event
and an example with a form and an input.

Example
...
<form action="">
    ...
    <input type="button" onclick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME'); return true;" value="Заказать" />
</form>
...

But I am using onClick and Button. When I write like this:
<Button onClick="ym(XXXXXX, 'reachGoal', 'TARGET_NAME)" ></Button>

The typescript says that
Mistake
Тип "string" не может быть назначен для типа "(event: MouseEvent) => void"


The fact is that several functions are still called by this button, so in any case I just can’t insert a line in React

Button
<Button onClick={()=> {
                  setCookie('startTime', +start);
                  props.setCountSubmit(props.countSubmit + 1);
                  setCookie('countSubmit', props.countSubmit);
                  props.setShowModalWindow(false);
                  props.setShowModalWindowThank(true);
                  postFormData(data}
}
></Button>


well, and accordingly, without a typescript, it also swears:
mistake

Error: Expected `onClick` listener to be a function, instead got a value of `string` type.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xenonhammer, 2020-08-27
@xenonhammer

To everyone who came on the same issue, here is the solution -
You can call such functions with a callback using quotes

onClick={()=> `ym(XXXXXX, 'reachGoal', 'TARGET_NAME)`}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question