Skip to content

refactor: depend on the position numbers instead of a memo helper - #213

Closed
Eliav2 wants to merge 1 commit into
mainfrom
refactor/drop-runtime-deps
Closed

refactor: depend on the position numbers instead of a memo helper#213
Eliav2 wants to merge 1 commit into
mainfrom
refactor/drop-runtime-deps

Conversation

@Eliav2

@Eliav2 Eliav2 commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Follow-up to #212. This commit was pushed to that branch but landed after it was merged, so it never reached main.

What it fixes

CodeRabbit's review point on #212, which was correct:

usePositionMemoize calls useRef from a callback and writes ref.current during render.

useLayoutEffect(callback, dependencies.map(usePositionMemoize)) calls a hook inside a .map() callback, and mutates the ref during render.

Why the simple version works

The comparison was only ever four numbers, so the four numbers can be the dependency array:

useLayoutEffect(() => { ... }, [startPos.x, startPos.y, startPos.right, startPos.bottom]);

That removes samePosition, usePositionMemoize and usePositionEffect outright rather than relocating the useRef. The effect fires on exactly the same condition as before. 31 lines deleted, 8 added.

History

The hook-in-a-callback predates all of this. The original was useLayoutEffect(callback, dependencies.map(useDeepCompareMemoize)) with a lodash isEqual inside; #212 swapped the equality function and kept the shape. It only existed to work around comparing an object that getElemPos rebuilds every render, which stops being a problem once the dependencies are primitives.

Verification

Rendered five prop combinations against the main build and this one and diffed the markup: byte identical. 53 library tests and 2 demo tests pass, type-check clean.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved arrow positioning updates when connected elements move.
    • Ensured start and end positions refresh reliably as individual coordinates change.

Review feedback on this PR.

usePositionMemoize called useRef from inside dependencies.map(), which is a
hook in a callback, and wrote ref.current during render. The pattern predates
this PR, but it only existed to work around comparing an object that
getElemPos rebuilds every render.

Since the comparison was four numbers, the four numbers can be the dependency
array. That removes samePosition, usePositionMemoize and usePositionEffect
outright, and the effect fires on exactly the same condition as before.

Rendered output still byte identical to main across all five cases.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codesandbox

codesandbox Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@netlify

netlify Bot commented Aug 8, 2026

Copy link
Copy Markdown

Deploy Preview for react-xarrows ready!

Name Link
🔨 Latest commit 632ff5c
🔍 Latest deploy log https://app.netlify.com/projects/react-xarrows/deploys/6a77abf804fa000008734e0e
😎 Deploy Preview https://deploy-preview-213--react-xarrows.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 32b939d5-bee7-4541-bb54-c29ab770f267

📥 Commits

Reviewing files that changed from the base of the PR and between 7465ae5 and 632ff5c.

📒 Files selected for processing (1)
  • src/Xarrow/useXarrowProps.ts

📝 Walkthrough

Walkthrough

The position comparison helpers were removed. Start and end position updates now use separate useLayoutEffect calls with individual coordinate dependencies.

Changes

Position tracking

Layer / File(s) Summary
Direct layout effects
src/Xarrow/useXarrowProps.ts
Removed custom position memoization helpers. Added separate layout effects for start and end coordinates. Each effect updates the cached position, marks the update, and refreshes state.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main refactor from a memoization helper to direct position-number dependencies.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/drop-runtime-deps

Comment @coderabbitai help to get the list of available commands.

@Eliav2

Eliav2 commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

Closing. Behavior is equivalent for all real coordinates, but the change is not worth the risk right now: it swaps === for Object.is on the position deps, which fires one extra render when a coordinate is exactly -0. Keeping the current memo helper.

@Eliav2 Eliav2 closed this Aug 8, 2026
@Eliav2
Eliav2 deleted the refactor/drop-runtime-deps branch August 8, 2026 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant