question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Implsible to use @reach/alert with React Context

See original GitHub issue

šŸ› Bug report

Current Behavior

The accessible div with role="alert|status" doesnā€™t have any content.

Itā€™s is not anywhere that youā€™re not supposed to pass as children anything that relies on a react context. This makes it impossible to use your awesome accessible alert component with most of css-in-js libraries.

Expected behavior

To render alert without throwing the errors.

Reproducible example

Notice how alert gets fired for the second time with undefined as a value CodeSandbox example

Suggested solution(s)

To either specify that you canā€™t use context consumers as children of @reach/alert component or to somehow prevent cloning passed children.

Additional context

The error occurs because of this render call https://github.com/reach/reach-ui/blob/a376daec462ccb53d33f4471306dff35383a03a5/packages/alert/src/index.tsx#L145-L149

Your environment

Software Name(s) Version
Reach Package Alert 0.10.0
React 16.12.0
Browser Any
Assistive tech none
Node any
npm/yarn any
Operating System any

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chaancecommented, Apr 14, 2020

Developers canā€™t read from context inside of an Alert because we arenā€™t using ReactDOM.createPortal(), weā€™re actually creating a couple of brand new React roots. We could use createPortal but then apps would need to render the entire app tree in an <AlertProvider>, or maybe thereā€™s a way with default context to do it, but we havenā€™t explored that yet. So, weā€™ll see how this goes. If it becomes a problem we can introduce a portal later.

0reactions
necinccommented, Apr 14, 2020

Then could you please tell if there were any limitations that did not allow us to use portals in the first place? In case somebody (maybe even myself) will stumble upon this issue and would like to open an MR with the fix so they have some context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not able to access react state using Context API - Stack Overflow
Try it <AlertContext.Provider value={{setMsg,alerts}}> {props.children} </AlertContext.Provider>.
Read more >
Context - React
Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it...
Read more >
Pitfalls of overusing React Context - LogRocket Blog
In this tutorial, we'll review the potential consequences of overusing React Context and discuss how to use it effectively in your next React...
Read more >
Alert - Reach UI
Accessible component to create alert messages for React. ... command line in your project directory, run npm install @reach/alert or yarn add @reach/alert...
Read more >
Why I never use React.useContext - Julianā€‹Garamendyā€‹.dev
This function returns a tuple with a provider and a custom hook. It's impossible to leak the Context, and therefore impossible to consume...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found