I’m pretty new to React and only just starting out. I’ve tried to find the answer but not sure if I’m searching for the wrong phrase or keyword(s).
Using https://ionicframework.com/docs/api/slides as an example. I want to apply the lockSwipes method and only allow the user to continue by pressing on a button with an onClick.
However, I just cannot figure out how to call them.
I’m not expecting code to be written for me. Happy for you to point me in the right direction and ill go readup but spent the past two evenings attempting and so far failing and loosing hope.
Thank you for getting back to me. However, it is throwing the following error
TS2322: Type 'MutableRefObject<HTMLIonSlideElement | undefined>' is not assignable to type '((instance: HTMLIonSlidesElement | null) => void) | RefObject<HTMLIonSlidesElement> | null | undefined'. Type 'MutableRefObject<HTMLIonSlideElement | undefined>' is not assignable to type 'RefObject<HTMLIonSlidesElement>'. Types of property 'current' are incompatible. Type 'HTMLIonSlideElement | undefined' is not assignable to type 'HTMLIonSlidesElement | null'. Type 'undefined' is not assignable to type 'HTMLIonSlidesElement | null'.
Sorry for the late reply.
However, after a bit of trial and error following the information youve provided I’ve successfully achieved what I was looking to do.
As I’m facing the same problem, would you mind sharing your final solution? I used the solution from @mirkonasato and have the Error Invalid Hook Call etc…
Doing some trial and error here but if you see this and mind sharing it might be most helpful thanks.
For some reason when in react and typescript and useRef i could not get rid of the error Object is possible null, not even using several ifs to make sure the value would never be null.
I ended up getting the HTML element and applying the methods directly.
Sounds like you have strict null checks enabled. That’s fine, you can access any element ref safely like this:
const MyComponent = () => {
const ref = useCallback((slidesRef) => {
if (slidesRef) {
// Can safely access the reference in here,
// and call any methods on this ref, like slidesRef.getSwiper()
}
}, [])
return (
<IonSlides ref={ref} />
)
}
@DevRenshaw The best way to call method is, via “ViewChild”. By using this you can inherit the “methods” for that particular component. (this is for Angular-Ionic)
import { Component, OnInit, ViewChild } from ‘@angular/core’;
@ViewChild(IonSlides) mySlide: IonSlides;
//Call this method from any events (click, keyup etc)
lockMySwipe(){
this.mySlide.lockSwipes(true);
}