UNPKG

908 BPlain TextView Raw
1'use strict';
2import { useEffect, useState } from 'react';
3import { cancelAnimation } from '../animation';
4import type { SharedValue } from '../commonTypes';
5import { makeMutable } from '../core';
6
7/**
8 * Lets you define [shared values](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#shared-value) in your components.
9 *
10 * @param initialValue - The value you want to be initially stored to a `.value` property.
11 * @returns A shared value with a single `.value` property initially set to the `initialValue` - {@link SharedValue}.
12 * @see https://docs.swmansion.com/react-native-reanimated/docs/core/useSharedValue
13 */
14export function useSharedValue<Value>(initialValue: Value): SharedValue<Value> {
15 const [mutable] = useState(() => makeMutable(initialValue));
16 useEffect(() => {
17 return () => {
18 cancelAnimation(mutable);
19 };
20 }, [mutable]);
21 return mutable;
22}