reach / reach-ui

The Accessible Foundation for React Apps and Design Systems
https://reacttraining.com/reach-ui/
MIT License
5.96k stars 561 forks source link

Context is unset for `@reach/alert` child components #875

Open chipit24 opened 2 years ago

chipit24 commented 2 years ago

🐛 Bug report

Current Behavior

When a component that uses context is a child of ReachAlert, then the context value is called a second time with undefined.

<div className="App">
  <TestContext.Provider value="Hello, world!">
    <ReachAlert>
      <ComponentThatUsesContext />
    </ReachAlert>
  </TestContext.Provider>
</div>

const ComponentThatUsesContext = () => {
  const text = useContext(TestContext);
  console.log("text", text);

  return <div>Text: {text}</div>;
};

Console output:

text Hello, world! 
text undefined

Without ReachAlert wrapping ComponentThatUsesContext, the console output is simply:

text Hello, world! 

Expected behavior

Context should not be set to undefined.

Reproducible example

CodeSandbox Template

Suggested solution(s)

I haven't looked at the source code so I'm not sure. Components that are children of ReachAlert should not have their context values changed by <ReachAlert>.

Additional context

I discovered this bug when using react-intl's FormattedMessage component with ReachAlert: CodeSandbox Template

<div className="App">
  <IntlProvider locale="en">
    <ReachAlert>
      <FormattedMessage id="hello-world" defaultMessage="Hello, world!" />
    </ReachAlert>
  </IntlProvider>
</div>

With that setup, I get the following error:

[React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.

Clearly, IntlProvider is set up correctly in the component tree so that is not the problem. When I remove ReachAlert the error is gone! The simpler case seems to reveal what might be happening: the context values under ReachAlert are somehow unset.

Your environment

(This is for my local machine where I can reproduce, clearly CodeSandbox uses different versions and it's still reproducible on there)

Software Name(s) Version
Reach Package @reach/alert 0.16.0
React react 17.0.1
Browser Chrome 96.0
Assistive tech n/a n/a
Node node 14.15.4
npm/yarn yarn 1.22.11
Operating System macOS Big Sur 11.6
chaance commented 2 years ago

The reason for this is because we are creating a new React root for our mirror. This causes some other problems as well, so I'm looking to fix this as time allows.