-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to type useRef? #74
Comments
Hi, you are dealing most probably with an array therefore you have to do the following thing: // init childRefs // set refs for swiping cards with button
const childRefs: RefObject<API>[] = useMemo(
() =>
Array(yourArray)
.fill(0)
.map((i) => createRef()),
[yourArray],
); // init fn for swipe for example with buttons // swipe function
const swipe = async (direction: Direction) => {
if (
canSwipe &&
currentIndex < yourArray.length &&
childRefs[currentIndex].current
) {
await childRefs[currentIndex].current.swipe(direction); // Swipe the card!
}
}; // ref on TinderCard
For more details check this example, you can easily see how to deal with it: |
I had this same issue. I think it would help a lot of |
@seeker-3 - what sort of magic did you use? I've resorted to going into the package folder and manually exporting them |
I started off trying to do some magic import type TinderCard from 'react-tinder-card'
type ExtractGenericFromRefObject<TRefObject> =
TRefObject extends RefObject<infer U> ? U : never
type TinderCardProps = Parameters<typeof TinderCard>[0]
export type API = ExtractGenericFromRefObject<TinderCardProps['ref']> Which successfully extracted the type, but for some reason, it would fuss at me when I tried importing the type Direction = 'left' | 'right' | 'up' | 'down'
export interface API {
swipe(dir?: Direction): Promise<void>
restoreCard(): Promise<void>
} |
Fair enough mate I don't blame you there, thanks :) |
same issue, please fix. wasted time on this. |
The fact that critical types are not exposed is a red flag for this library. The necessary work that needs to be done is to go through the whole library and change things here and there to follow typescript best practices, and then also refactor the example library to use typescript with zero eslint errors/warnings on the strictest settings feasible |
With npmjs libraries, you take what you can get. Expecting libraries to have clean code is a bit foolish, better to just be thankful it exists at all! 🙏 |
Hi
I have typescript app and I want to add buttons like in https://github.com/3DJakob/react-tinder-card/blob/master/index.js here.
The problem is that I can not properly type useRef hook to use swipe() method.
const tinderCardRef = useRef(null);
tinderCardRef.current.swipe();
Thanks
The text was updated successfully, but these errors were encountered: