fix(react): ensure useDocumentProjection reuses state source for all strings #669
+242
−40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Description
useDocumentProjectionwas actually a bit finicky about theprojectionbeing passed to it. We had a ticket about it looping foreverMy best guess about what was happening was this:
useDocumentProjectionuseDocumentProjectionwould then callgetProjectionState. It always returns a newStateSource, which returns a newsubscribefunctionuseSyncExternalStorereceives the newsubscribefunction, triggering a re-renderThis PR adds some memoization to maintain consistency in projection strings and state sources.
We didn't catch this in casual clicking around because
defineProjectionwas doing some of the memoization work for us, I think. To address this, I added some common ways people would useuseDocumentProjectionand also some e2e tests against it.What to review
Any risks of over memoization or staleness?
Testing
e2e tests (I think this is hard to capture in React tests and we still have 100% coverage).
To see the OLD behavior,
git fetchgit checkout maingit checkout origin/fix/sdk-660/ensure-doc-projections-reuse-state-source apps/kitchensink-react/src/DocumentCollection/DocumentProjectionRoute.tsxpnpm devThis PR fixes that.