Skip to content

Commit

Permalink
Fix Static rendering stale items on unmount (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes authored Aug 18, 2020
1 parent 8d8bbdb commit 6ae66e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jobs:
- run: npm test -- --serial
env:
FORCE_COLOR: true
CI: false
4 changes: 2 additions & 2 deletions src/components/Static.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useMemo, useState, useEffect} from 'react';
import React, {useMemo, useState, useLayoutEffect} from 'react';
import type {ReactNode} from 'react';
import type {Styles} from '../styles';

Expand Down Expand Up @@ -41,7 +41,7 @@ const Static = <T,>(props: Props<T>) => {
return items.slice(index);
}, [items, index]);

useEffect(() => {
useLayoutEffect(() => {
setIndex(items.length);
}, [items.length]);

Expand Down
22 changes: 22 additions & 0 deletions test/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,28 @@ test('skip previous output when rendering new static output', t => {
t.is(stdout.write.lastCall.args[0], 'A\nB\n');
});

test('render only new items in static output on final render', t => {
const stdout = createStdout();

const Dynamic: FC<{items: string[]}> = ({items}) => (
<Static items={items}>{item => <Text key={item}>{item}</Text>}</Static>
);

const {rerender, unmount} = render(<Dynamic items={[]} />, {
stdout,
debug: true
});

t.is(stdout.write.lastCall.args[0], '');

rerender(<Dynamic items={['A']} />);
t.is(stdout.write.lastCall.args[0], 'A\n');

rerender(<Dynamic items={['A', 'B']} />);
unmount();
t.is(stdout.write.lastCall.args[0], 'A\nB\n');
});

// See https://github.com/chalk/wrap-ansi/issues/27
test('ensure wrap-ansi doesn’t trim leading whitespace', t => {
const output = renderToString(<Text color="red">{' ERROR '}</Text>);
Expand Down

0 comments on commit 6ae66e6

Please sign in to comment.