We use Reakit dialogs to prompt users to take an action in our React web app.
On some pages, we have specific text related to the action and would like to render this specific content in the dialog. On all other pages, we want to fall back to generic text.
Our simplified component hierarchy for generic pages looks like:
<BaseLayout>
...
</BaseLayout>
and for a page where we want to show specific text,
<BaseLayout>
...
<SpecificPage/>
...
</BaseLayout>
What we'd like to happen is:
- On pages that render the
SpecificPage
component, the Dialog appears with the specific text - On pages that do not render the
SpecificPage
component, the Dialog appears with the fallback generic text
Our approach was to have the SpecificPage
component render a Dialog with the page-specific text, and the BaseLayout
component render a Dialog with the generic fallback text, but this approach isn't ideal -- users see a flash of the BaseLayout
dialog before the SpecificPage
dialog is rendered. Is there any way to define a single component that is "overridden" by descendants in the component hierarchy, or other way to achieve this conditional rendering?
See https://github.com/ariakit/ariakit/discussions/1266#discussioncomment-2617748 for a solution and CodeSandbox that solves this problem well using the Constate library.