Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-spread-undefined-ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/react": patch
---

fix: avoid crash when spread undefined ref
2 changes: 1 addition & 1 deletion packages/react/runtime/src/backgroundSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class BackgroundSnapshotInstance {
v.__snapshot_def.refAndSpreadIndexes?.forEach((i) => {
const value = v.__values![i] as unknown;
if (value && (typeof value === 'object' || typeof value === 'function')) {
if ('__spread' in value && 'ref' in value) {
if ('__spread' in value && 'ref' in value && value.ref) {
applyRef(value.ref as Ref, null);
} else if ('__ref' in value) {
applyRef(value as Ref, null);
Expand Down
22 changes: 22 additions & 0 deletions packages/react/testing-library/src/__tests__/ref.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,26 @@ describe('element ref', () => {
expect(lynx.getNativeApp().callLepusMethod).toBeCalledTimes(2);
vi.resetAllMocks();
});

it('spread undefined ref should work', () => {
let setProps, setShowChild;
const Child = () => {
const [props, _setProps] = useState({ ref: undefined });
setProps = _setProps;

return <view {...props} />;
};
const App = () => {
let [showChild, _setShowChild] = useState(true);
setShowChild = _setShowChild;

return showChild && <Child />;
};

render(<App />);

act(() => {
setShowChild(false);
});
});
});
Loading