Skip to content

Commit c635f5b

Browse files
authored
Merge pull request #16 from antoinetissier/fix-race-conditions-when-swapping-nodes
2 parents 088033e + 51b95b8 commit c635f5b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class OutPortal<C extends Component<any>> extends React.PureComponent<OutPortalP
199199

200200
// If we're switching portal nodes, we need to clean up the current one first.
201201
if (this.currentPortalNode && node !== this.currentPortalNode) {
202-
this.currentPortalNode.unmount();
202+
this.currentPortalNode.unmount(this.placeholderNode.current!);
203203
this.currentPortalNode = node;
204204
}
205205

stories/html.stories.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,40 @@ storiesOf('Portals', module)
226226
<Parent />
227227
</div>;
228228
})
229+
.add("swap nodes between different locations", () => {
230+
const numberOfNodes = 5;
231+
const initialOrder = [];
232+
for (let i = 0; i < numberOfNodes; i++) {
233+
initialOrder[i] = i;
234+
}
235+
236+
const ExampleContent = ({ content }) => String(content);
237+
238+
const ChangeLayoutWithoutUnmounting = () => {
239+
const nodes = React.useMemo(
240+
() => initialOrder.map(createHtmlPortalNode),
241+
[]
242+
);
243+
const [order, setOrder] = React.useState(initialOrder);
244+
return (
245+
<div>
246+
<button onClick={() => setOrder(order.slice().reverse())}>
247+
Click to reverse the order
248+
</button>
249+
{nodes.map((node, index) => (
250+
<InPortal node={node}>
251+
<ExampleContent content={index} />
252+
</InPortal>
253+
))}
254+
{order.map((position) => (
255+
<OutPortal node={nodes[position]} />
256+
))}
257+
</div>
258+
);
259+
};
260+
261+
return <ChangeLayoutWithoutUnmounting />;
262+
})
229263
.add('Example from README', () => {
230264
const MyExpensiveComponent = () => 'expensive!';
231265

0 commit comments

Comments
 (0)