Soy nuevo en rxjs, cuando intento crear un evento para incrementButton, arroja un error como
No overload matches this call. The last overload gave the following error. Argument of type 'Element | null' is not assignable to parameter of type 'JQueryStyleEventEmitter<any, unknown> | ArrayLike<JQueryStyleEventEmitter<any, unknown>>'. Type 'null' is not assignable to type 'JQueryStyleEventEmitter<any, unknown> | ArrayLike<JQueryStyleEventEmitter<any, unknown>>'. TS2769En el siguiente fragmento Line:8 Col:35 incrementButton throw error
import { Fragment, useState } from "react"; import { fromEvent } from "rxjs"; const SimpleCounter = () => { const [result, setResult] = useState(0); const incrementButton = document.querySelector("incBtn"); const increaseCount = fromEvent(incrementButton, "click"); return ( <Fragment> <h1>{result}</h1> <button id="incBtn">Increase</button> </Fragment> ); };Intenté escribir el tipo como se muestra a continuación, pero falló
const increaseCount = fromEvent(<JqueryStyleEventEmitter> <unknow> incrementButton, "click"); const increaseCount = fromEvent(incrementButton as JqueryStyleEventEmitter as unknown, "click");Hay 2 cambios que he hecho a mi código. Ahora está funcionando.
const incrementButton = document.querySelector( "#incBtn" ) as HTMLButtonElement; useEffect(() => { const incrementButton = document.querySelector( "#incBtn" ) as HTMLButtonElement; const increaseCount = fromEvent(incrementButton, "click"); increaseCount.subscribe(() => {setResult(result++)}); });