Functions in useContext API
See original GitHub issueHello,
I have a Context API component to show/hide a modal. I’m using something very similar to the Toast example from here: Ponyfoo Toast
WDYR gives me this error:
How can I avoid this re-render? Or it is normal react behavior?
All functions are created using useCallback, I’ve already tried adding memo to both the <Header />
(the component that I tested WDYR) and the <Context.Provider />
Any ideas?
Thanks in advance
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
Hooks API Reference - React
They let you use state and other React features without writing a class. This page describes the APIs for the built-in Hooks in...
Read more >How can ı pass function with state for react useContext
The React Context is a mechanism for passing data through a React node tree without having to pass the props manually.
Read more >A Guide to React Context and useContext() Hook
The React context provides data to components no matter how deep they are in the components tree. The context is used to manage...
Read more >How To Work with Context API in React and React Hooks
Your return statement applies the background color in your application. When a change triggers, the useContext() method will subscribe update ...
Read more >React useContext Hook - W3Schools
React Context is a way to manage state globally. It can be used together with the useState Hook to share state between deeply...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ok so here
{ showModal, hideModal }
is a new object on everyModalManager
re-render that triggers a re-render of every offspring that usesModalContext
what i suggest you to do is something like:
and then:
I didnt run it but i think you’ll get the idea behind it.
the main point is to make sure
value
is always exactly the same object and not a new one on everyModalManager
re-renderIMHO I think it is better to put the useMemo outside of the hook in ModalManager since it’s not related to the hook logic and more robust