Skip to content

fix: avoid render-phase shared value read in getCurrentIndex (fix #829)#895

Closed
dohooo wants to merge 1 commit into
mainfrom
caspian/fix-829-render-shared-value-warning
Closed

fix: avoid render-phase shared value read in getCurrentIndex (fix #829)#895
dohooo wants to merge 1 commit into
mainfrom
caspian/fix-829-render-shared-value-warning

Conversation

@dohooo

@dohooo dohooo commented Mar 2, 2026

Copy link
Copy Markdown
Owner

Summary

This PR fixes the render-phase shared value warning reported in #829:

Reading from value during component render...

The warning was triggered when getCurrentIndex() was called during React render (for example through an imperative ref read during rerender).

1) Trigger chain

  • Carousel exposes imperative getCurrentIndex() from useCarouselController.
  • Before this fix, getCurrentIndex() read index.value directly.
  • If user code calls ref.current?.getCurrentIndex() during component render, Reanimated strict logger reports render-phase shared value access.

2) Fix strategy

  • Keep a JS-side index snapshot (sharedIndex.current) as the source for imperative reads.
  • Change getCurrentIndex() to read sharedIndex.current instead of index.value.
  • Add syncSharedIndex() and update it in next/prev/to paths so imperative commands remain consistent immediately.
  • Keep animation/gesture logic unchanged (no warning silencing, no logger suppression).

3) Regression test

Added a warning-level regression test in src/components/Carousel.test.tsx:

  • Captures Reanimated logger output for Reading from value during component render.
  • Reproduces minimal scenario: rerender + imperative getCurrentIndex() read during render.
  • Asserts no warning is emitted after the fix.

This test is Red before the fix and Green after.

4) Validation

Executed:

yarn test src/components/Carousel.test.tsx src/hooks/useCarouselController.test.tsx src/components/Pagination/Pagination.test.tsx --runInBand --verbose=false

Result:

  • 3 test suites passed
  • 80 tests passed

Notes

  • This does not mute or disable Reanimated strict warnings.
  • Existing interaction behavior remains covered by controller and carousel tests.

Closes #829.

@vercel

vercel Bot commented Mar 2, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-native-reanimated-carousel Building Building Preview, Comment Mar 2, 2026 8:49am

Request Review

@changeset-bot

changeset-bot Bot commented Mar 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 913c603

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Mar 2, 2026
@dosubot

dosubot Bot commented Mar 2, 2026

Copy link
Copy Markdown

Related Documentation

Checked 4 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@codecov

codecov Bot commented Mar 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.44%. Comparing base (5289d9d) to head (913c603).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #895      +/-   ##
==========================================
+ Coverage   84.06%   84.44%   +0.37%     
==========================================
  Files          37       37              
  Lines        1042     1048       +6     
  Branches      364      364              
==========================================
+ Hits          876      885       +9     
  Misses         67       67              
+ Partials       99       96       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

1 participant