Skip to content

Commit

Permalink
Fixes Reanimated 2 detection (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Oct 5, 2022
1 parent 663c1b9 commit 9b36c98
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions package/src/external/reanimated/useSharedValueEffect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { useEffect, useMemo } from "react";

// @ts-ignore
let Reanimated: typeof import("react-native-reanimated") | undefined;
let Core: // @ts-ignore
typeof import("react-native-reanimated/lib/reanimated2/core") | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let Reanimated: any;

try {
Reanimated = require("react-native-reanimated");
Core = require("react-native-reanimated/lib/reanimated2/core");
} catch (e) {
// Ignore
}
Expand All @@ -36,29 +30,15 @@ export const useSharedValueEffect = <T = number>(
...values: SharedValueTypeWrapper<T>[]
) => {
const input = useSharedValueWrapper(0);
const { runOnJS, startMapper, stopMapper } = useMemo(() => {
if (Reanimated && Core) {
const { runOnJS } = Reanimated;
const { startMapper, stopMapper } = Core;
return { runOnJS, startMapper, stopMapper };
} else {

useEffect(() => {
if (!Reanimated) {
console.warn(
"Reanimated was not found and the useSharedValueEffect hook will have no effect."
);
return {
runOnJS: undefined,
startMapper: undefined,
stopMapper: undefined,
};
}
}, []);
} else {
const { runOnJS, startMapper, stopMapper } = Reanimated;

useEffect(() => {
if (
startMapper !== undefined &&
runOnJS !== undefined &&
stopMapper !== undefined
) {
// Start a mapper in Reanimated
const mapperId = startMapper(
() => {
Expand All @@ -77,5 +57,5 @@ export const useSharedValueEffect = <T = number>(
}
return () => {};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [input, runOnJS, startMapper, stopMapper, value, ...values]);
}, [input, value, ...values]);
};

0 comments on commit 9b36c98

Please sign in to comment.