Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Defer adding the container to the DOM until componentDidMount. Fixes consent form display issues #338

Conversation

shofman
Copy link
Contributor

@shofman shofman commented Jun 9, 2023

In NextJS, if the reactStrictMode is true, appending the container in the constructor does not render the children.

Should fix #303

I have created a playground to showcase the behavior https://github.com/shofman/nextjs-consent-example. Switching the node_module to point to my branch locally fixs the issue.

When reactStrictMode is true in NextJs, we mount and unmount the component multiple times. The timing of this leaves an empty <div> on the page without children. This is what prevents the consent form from appearing in NextJS.

If we defer attaching the container until componentDidMount, React cleans up the div properly and ensures that the children are attached properly (displaying the consent dialog).

Disclaimer - I have only tested this in a NextJs context, and not in an ordinary React project.

Copy link
Contributor

@edsonjab edsonjab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change works on different implementations (ReactJs, NextJs, Standalone) and fix the issues #278 and #303

@nd4p90x
Copy link
Collaborator

nd4p90x commented Jun 21, 2023

@shofman please update your branch so we can move forward with merging your PR. Thank you.

…tJS, if the reactStrictMode is true, appending the container in the constructor does not render the children
@shofman shofman force-pushed the fix-consent-manager-not-working-with-nextjs branch from 6df2215 to 6601c38 Compare June 22, 2023 11:27
@shofman
Copy link
Contributor Author

shofman commented Jun 23, 2023

Hey @nd4p90x - I've updated my branch, but it seems that something has changed with the builds. I see that CircleCI was removed - does the above message still make sense? ci/circleci: build Expected — Waiting for status to be reported.

Let me know if I need to do anything. Otherwise, I'll wait for a maintainer to approve

@nd4p90x
Copy link
Collaborator

nd4p90x commented Jun 23, 2023

@shofman all good, we are going away from circleci. Thank you

@MichaelGHSeg MichaelGHSeg merged commit 0670b5f into segmentio:master Jun 28, 2023
1 check passed
@shofman shofman deleted the fix-consent-manager-not-working-with-nextjs branch February 19, 2024 20:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issues with Next 13
4 participants