Skip to content

Commit

Permalink
Replace useRef with useState for useSharedValue
Browse files Browse the repository at this point in the history
The general idea here is that we can avoid the makeMutable call on every
single render.  This change is also compatible with hot reloading.
  • Loading branch information
amadeus committed Dec 2, 2023
1 parent c6ddd55 commit 3a642cd
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/reanimated2/hook/useSharedValue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
import { useEffect, useRef } from 'react';
import { useEffect, useState } from 'react';
import { cancelAnimation } from '../animation';
import type { SharedValue } from '../commonTypes';
import { makeMutable } from '../core';
Expand All @@ -15,19 +15,11 @@ export function useSharedValue<Value>(
initialValue: Value,
oneWayReadsOnly = false
): SharedValue<Value> {
const ref = useRef<SharedValue<Value>>(
makeMutable(initialValue, oneWayReadsOnly)
);

if (ref.current === null) {
ref.current = makeMutable(initialValue, oneWayReadsOnly);
}

const [mutable] = useState(() => makeMutable(initialValue, oneWayReadsOnly));
useEffect(() => {
return () => {
cancelAnimation(ref.current);
cancelAnimation(mutable);
};
}, []);

return ref.current;
return mutable;
}

0 comments on commit 3a642cd

Please sign in to comment.