Skip to content

Commit d38630f

Browse files
committed
Support Iterables in Flight
1 parent 5c633a4 commit d38630f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,34 @@ describe('ReactFlight', () => {
166166
expect(ReactNoop).toMatchRenderedOutput(<span>Hello, Seb Smith</span>);
167167
});
168168

169+
it('can render an iterable as an array', async () => {
170+
function ItemListClient(props) {
171+
return <span>{props.items}</span>;
172+
}
173+
const ItemList = clientReference(ItemListClient);
174+
175+
function Items() {
176+
const iterable = {
177+
[Symbol.iterator]: function* () {
178+
yield 'A';
179+
yield 'B';
180+
yield 'C';
181+
},
182+
};
183+
return <ItemList items={iterable} />;
184+
}
185+
186+
const model = <Items />;
187+
188+
const transport = ReactNoopFlightServer.render(model);
189+
190+
await act(async () => {
191+
ReactNoop.render(await ReactNoopFlightClient.read(transport));
192+
});
193+
194+
expect(ReactNoop).toMatchRenderedOutput(<span>ABC</span>);
195+
});
196+
169197
it('can render a lazy component as a shared component on the server', async () => {
170198
function SharedComponent({text}) {
171199
return (

packages/react-server/src/ReactFlightServer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
} from './ReactFlightNewContext';
7474

7575
import {
76+
getIteratorFn,
7677
REACT_ELEMENT_TYPE,
7778
REACT_FORWARD_REF_TYPE,
7879
REACT_FRAGMENT_TYPE,
@@ -1053,6 +1054,12 @@ export function resolveModelToJSON(
10531054
}
10541055
return (undefined: any);
10551056
}
1057+
if (!isArray(value)) {
1058+
const iteratorFn = getIteratorFn(value);
1059+
if (iteratorFn) {
1060+
return Array.from((value: any));
1061+
}
1062+
}
10561063

10571064
if (__DEV__) {
10581065
if (value !== null && !isArray(value)) {

0 commit comments

Comments
 (0)