Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

PropProvider Context + Portal Fixes for React 16 #454

Merged
merged 1 commit into from Jan 25, 2019
Merged

Conversation

ItsJonQ
Copy link
Contributor

@ItsJonQ ItsJonQ commented Jan 25, 2019

PropProvider Context + Portal Fixes for React 16

screen recording 2019-01-25 at 04 50 pm

(GIF: Demo shows a iFrame rendered, Portal'ed component with buttons correctly rendering values defined by a parent PropProvider. In other words.. super hard mode!)

This update fixes the transfer for PropProvider's context to content rendered
with Portal.

contextConnect

During a React 16 update, we discovered that PropProvider values defined
outside of the portal'ed content were not being transferred. This is
due to how ReactDOM.unstable_renderSubtreeIntoContainer renders the content
vs. React 16's new context system.

The solution was to create a new HOC (called contextConnect), which provides
a component directly with the values from the closest PropProvider. Enhancing
Portal with this new HOC, I'm able to pass the values through the children
elements. Things work as expected!

ReactDOM.createPortal?

I've experimented with this solution. It kinda works with HSDS's Portal mechanism. However, the
requirement to support React 15, iFrames, and programatic open/close makes this (native) solution
very unstable.

We should consider exploring this once we're fully migrated to React 16 :)

Refactors

I made some refactors to the Portal file structure during this update. I decided against converting
things to Typescript (for now).

Testing

This update has been manually tested in React 15 and React 16. Everything appears to be working,
including supported for nested iFrame rendering.

This update fixes the transfer for `PropProvider`'s context to content rendered
with `Portal`.

During a React 16 update, we discovered that `PropProvider` values defined
outside of the portal'ed content were not being transferred. This is
due to how `ReactDOM.unstable_renderSubtreeIntoContainer` renders the content
vs. React 16's new context system.

The solution was to create a new HOC (called `contextConnect`), which provides
a component directly with the values from the closest `PropProvider`. Enhancing
`Portal` with this new HOC, I'm able to pass the values through the children
elements. Things work as expected!
@ItsJonQ ItsJonQ self-assigned this Jan 25, 2019
@ItsJonQ ItsJonQ requested a review from plbabin January 25, 2019 21:58
@ItsJonQ ItsJonQ changed the title PropProvider Context + Portal Fixes PropProvider Context + Portal Fixes for React 16 Jan 25, 2019
@ItsJonQ ItsJonQ merged commit 8d4c65d into master Jan 25, 2019

this.portal = ReactDOM.unstable_renderSubtreeIntoContainer(
this,
<PropProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the magical bridge right here that transfers the PropProvider context props!

@coveralls
Copy link

Pull Request Test Coverage Report for Build 1536

  • 86 of 86 (100.0%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 1535: 0.0%
Covered Lines: 6044
Relevant Lines: 6044

💛 - Coveralls

@ItsJonQ ItsJonQ deleted the portal-context-fix branch January 28, 2019 19:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants