Skip to content

perf: Replace useSharedValue with useMutableValue#408

Merged
MatiPl01 merged 2 commits intomainfrom
perf/use-mutables-where-possible
Jun 21, 2025
Merged

perf: Replace useSharedValue with useMutableValue#408
MatiPl01 merged 2 commits intomainfrom
perf/use-mutables-where-possible

Conversation

@MatiPl01
Copy link
Copy Markdown
Owner

@MatiPl01 MatiPl01 commented Jun 21, 2025

Description

This PR replaces all useSharedValue usages with useMutableValue. useSharedValue is heavier because of the cleanup useEffect that cancels currently running animation if there is any:

export function useSharedValue<Value>(initialValue: Value): SharedValue<Value> {
  const [mutable] = useState(() => makeMutable(initialValue));
  useEffect(() => {
    return () => {
      cancelAnimation(mutable);
    };
  }, [mutable]);
  return mutable;
}

I don't need this behavior as I never use infinite animations and, in most cases, don't use animations at all, rather update value by myself. I only use animations for items reordering and the drop indicator, but since they are just short withTiming animations, the cleanup is not necessary. Even if the animation keeps running after the view was unmounted, it runs for a brief while, so there is no performance benefit in eager cleanup and it's better to reduce the number of unnecessary useEffect hooks.

@MatiPl01 MatiPl01 self-assigned this Jun 21, 2025
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
react-native-sortables-docs ⬜️ Ignored (Inspect) Visit Preview Jun 21, 2025 3:36pm

@MatiPl01 MatiPl01 requested a review from Copilot June 21, 2025 15:37
@MatiPl01 MatiPl01 marked this pull request as ready for review June 21, 2025 15:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors various components and hooks to replace the use of useSharedValue with a custom useMutableValue hook for lazy initialization and improved performance.

  • Introduce useMutableValue for lazy creation of reanimated mutable values
  • Update useAnimatedDebounce and other hooks/providers to use useMutableValue
  • Remove all useSharedValue imports and replace with the new hook

Reviewed Changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.

File Description
packages/react-native-sortables/src/utils/reanimated/useMutableValue.ts Adds useMutableValue with lazy initialization
packages/react-native-sortables/src/utils/reanimated/useAnimatedDebounce.ts Switches from useSharedValue to useMutableValue
All provider, layout, hook, and component files Replace useSharedValue imports/usage
Comments suppressed due to low confidence (2)

packages/react-native-sortables/src/utils/reanimated/useMutableValue.ts:1

  • [nitpick] Consider adding a JSDoc comment above useMutableValue to explain its purpose, parameters, and return value, improving maintainability and discoverability.
import { useState } from 'react';

packages/react-native-sortables/src/utils/reanimated/useMutableValue.ts:1

  • Add unit tests for useMutableValue to verify that the mutable value is initialized only once and that updates to .value persist across renders.
import { useState } from 'react';

@MatiPl01 MatiPl01 merged commit 6994c7a into main Jun 21, 2025
7 checks passed
@MatiPl01 MatiPl01 deleted the perf/use-mutables-where-possible branch June 21, 2025 15:41
MatiPl01 pushed a commit that referenced this pull request Jul 29, 2025
# [1.8.0](v1.7.1...v1.8.0) (2025-07-29)

### Bug Fixes

* Allow disabling default layer provider ([#424](#424)) ([ded5c92](ded5c92)), closes [#417](#417) [#36877](https://github.com/MatiPl01/react-native-sortables/issues/36877)
* Buggy grid swapping in some edge cases ([#414](#414)) ([3617f86](3617f86))
* DragProvider drag end index assignment ([#411](#411)) ([69ad8a6](69ad8a6))
* Hiding of the sortable item when teleported ([#436](#436)) ([4c3796f](4c3796f))
* Invalid flex container height on initial render and other small issues ([#437](#437)) ([48e905b](48e905b))
* Invalid ScrollView position on input focus ([#431](#431)) ([2693234](2693234)), closes [#ef4444](https://github.com/MatiPl01/react-native-sortables/issues/ef4444)
* Long drop duration reordering issues ([#406](#406)) ([ace7037](ace7037))
* Multiple minor improvements ([#439](#439)) ([f9d83e3](f9d83e3))
* Paper flickering and positioning issues ([#426](#426)) ([744e291](744e291))
* Teleported active item flickering ([#405](#405)) ([1413af2](1413af2))

### Features

* Base multi zone provider ([#409](#409)) ([56f0ef3](56f0ef3))
* Base zone with event callbacks ([#413](#413)) ([e51ca61](e51ca61))
* Fixed items support in sortable flex ([#416](#416)) ([1d23fcc](1d23fcc)), closes [#374](#374)
* Max overscroll settings ([#423](#423)) ([167dc4e](167dc4e)), closes [#419](#419)
* Separate controlled item dimensions from measured dimensions ([#433](#433)) ([cad95e5](cad95e5))

### Performance Improvements

* Merge item decoration styles with position styles ([#407](#407)) ([5066edd](5066edd))
* More performant items reordering ([#435](#435)) ([be78141](be78141))
* Reduce the number of unnecessary onLayout calls ([#434](#434)) ([ce56159](ce56159)), closes [#431](#431)
* Replace useSharedValue with useMutableValue ([#408](#408)) ([6994c7a](6994c7a))
@MatiPl01
Copy link
Copy Markdown
Owner Author

🎉 This issue has been resolved in version 1.8.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants