@kietpt2003/react-native-core-ui
Version:
React Native Core UI components by KietPT
56 lines (55 loc) • 2.27 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { useSharedValue, } from "react-native-reanimated";
import { DEFAULT_PROPS } from "../../constants/DraggableFlatList/constants";
import { useProps } from "./propsContext";
const RefContext = React.createContext(undefined);
export default function RefProvider({ children, flatListRef, }) {
const value = useSetupRefs({ flatListRef });
return _jsx(RefContext.Provider, { value: value, children: children });
}
export function useRefs() {
const value = React.useContext(RefContext);
if (!value) {
throw new Error("useRefs must be called from within a RefContext.Provider!");
}
return value;
}
function useSetupRefs({ flatListRef: flatListRefProp, }) {
const props = useProps();
const { animationConfig = DEFAULT_PROPS.animationConfig } = props;
const propsRef = React.useRef(props);
propsRef.current = props;
const animConfig = React.useMemo(() => (Object.assign(Object.assign({}, DEFAULT_PROPS.animationConfig), animationConfig)), [animationConfig]);
const animationConfigRef = useSharedValue(animConfig);
React.useEffect(() => {
animationConfigRef.value = animConfig;
}, [animConfig]);
const cellDataRef = React.useRef(new Map());
const keyToIndexRef = React.useRef(new Map());
const containerRef = React.useRef(null);
const flatlistRefInternal = React.useRef(null);
const flatlistRef = flatListRefProp || flatlistRefInternal;
const scrollViewRef = React.useRef(null);
// useEffect(() => {
// // This is a workaround for the fact that RN does not respect refs passed in
// // to renderScrollViewComponent underlying ScrollView (currently not used but
// // may need to add if we want to use reanimated scrollTo in the future)
// //@ts-ignore
// const scrollRef = flatlistRef.current?.getNativeScrollRef();
// if (!scrollViewRef.current) {
// //@ts-ignore
// scrollViewRef(scrollRef);
// }
// }, []);
const refs = React.useMemo(() => ({
animationConfigRef,
cellDataRef,
containerRef,
flatlistRef,
keyToIndexRef,
propsRef,
scrollViewRef,
}), []);
return refs;
}