-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid remount of
RouterTab
with forceRender={true}
when `RouterTa…
…bs` are used inside a `Stack` (#1439) Remounting causes local state to be lost, including final-form values. Problem was the missing `StackBreadcrumb` wrapper, as react remounts in that case (https://legacy.reactjs.org/docs/reconciliation.html#elements-of-different-types) --------- Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
- Loading branch information
1 parent
607b620
commit 25daac0
Showing
4 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/admin": patch | ||
--- | ||
|
||
Avoid remount of `RouterTab` with `forceRender={true}` when `RouterTabs` are used inside a `Stack` |
59 changes: 59 additions & 0 deletions
59
packages/admin/admin-stories/src/admin/tabs/RouterTabsInStack.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { RouterTab, RouterTabs, Stack, StackPage, StackSwitch } from "@comet/admin"; | ||
import { storiesOf } from "@storybook/react"; | ||
import * as React from "react"; | ||
|
||
import { storyRouterDecorator } from "../../story-router.decorator"; | ||
|
||
const mountCount: Record<string, number> = {}; | ||
|
||
function RenderCount(props: { name: string }) { | ||
React.useEffect(() => { | ||
mountCount[props.name] = (mountCount[props.name] || 0) + 1; | ||
}, [props.name]); | ||
const renderCount = React.useRef(0); | ||
renderCount.current++; | ||
return ( | ||
<div> | ||
Render count {props.name}: {renderCount.current} | ||
</div> | ||
); | ||
} | ||
|
||
function PrintMountCount() { | ||
const [, rerender] = React.useState(0); | ||
React.useEffect(() => { | ||
const timer = setInterval(() => { | ||
rerender(new Date().getTime()); | ||
}, 100); | ||
return () => clearInterval(timer); | ||
}, []); | ||
return <div>Mount count: {JSON.stringify(mountCount)}</div>; | ||
} | ||
|
||
function Story() { | ||
return ( | ||
<Stack topLevelTitle="Nested Stack"> | ||
<PrintMountCount /> | ||
<StackSwitch> | ||
<StackPage name="xxx"> | ||
<RenderCount name="StackPage" /> | ||
<RouterTabs> | ||
<RouterTab label="Foo" path="" forceRender={true}> | ||
Foo | ||
<RenderCount name="Foo" /> | ||
</RouterTab> | ||
<RouterTab label="Bar" path="/bar" forceRender={true}> | ||
Bar | ||
<RenderCount name="Bar" /> | ||
</RouterTab> | ||
</RouterTabs> | ||
</StackPage> | ||
<StackPage name="yyy">yyy</StackPage> | ||
</StackSwitch> | ||
</Stack> | ||
); | ||
} | ||
|
||
storiesOf("@comet/admin/tabs", module) | ||
.addDecorator(storyRouterDecorator()) | ||
.add("RouterTabs in Stack", () => <Story />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters