Comments (6)
ok so here
const ModalManager = ({ children }) => {
const { show, hideModal, showModal } = useModalManager();
return (
<ModalContext.Provider value={{ showModal, hideModal }}>
{children}
{show && <Modal onDismiss={hideModal} />}
</ModalContext.Provider>
);
};
{ showModal, hideModal }
is a new object on every ModalManager
re-render that triggers a re-render of every offspring that uses ModalContext
what i suggest you to do is something like:
const useModalManager = () => {
const [show, setShow] = useState(false);
const showModal = useCallback(() => {
setShow(true);
}, []);
const hideModal = useCallback(() => {
setShow(false);
}, []);
const actions = useMemo(() => ({
showModal, hideModal
}), [])
return {
show,
actions,
};
};
and then:
const ModalManager = ({ children }) => {
const { show, actions } = useModalManager();
return (
<ModalContext.Provider value={actions}>
{children}
{show && <Modal onDismiss={actions.hideModal} />}
</ModalContext.Provider>
);
};
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 every ModalManager
re-render
from why-did-you-render.
a hook makes components re-render "from the inside" of the component, so MEMO won't help.
what you need to do is to ensure, what ever passes
{hideAddressModal, showAddressModal}
is memoized.
or everytime you get to <Context hideAddressModal=...... />
every single component that depends on this context will be re-rendered. including pure components.
You can, for example, wrap it in a Memoized component.
const MemoizedContext = React.memo(
() => <Context ... />
)
from why-did-you-render.
IMHO 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
const useModalManager = () => {
const [show, setShow] = useState(false);
const showModal = useCallback(() => {
setShow(true);
}, []);
const hideModal = useCallback(() => {
setShow(false);
}, []);
return {
show,
showModel,
hideModel
};
};
const ModalManager = ({ children }) => {
const { show, showModel, hideModel } = useModalManager();
const actions = useMemo(() => ({
showModal, hideModal
}), [showModal, hideModal])
return (
<ModalContext.Provider value={actions}>
{children}
{show && <Modal onDismiss={hideModal} />}
</ModalContext.Provider>
);
};
from why-did-you-render.
@vzaidman Thank you for your answer.
I've created a very simple CRA app to show how I'm doing it. My context already has a memo around it, and even still, it re-renders the <Header>
every time.
Any suggestions on what I'm doing wrong?
Thanks again.
from why-did-you-render.
@vzaidman That worked like a charm! Thank you very much. =)
from why-did-you-render.
Sure :)
from why-did-you-render.
Related Issues (20)
- Issues with Parcel? HOT 3
- use React.lazy warp function, wdyr not work.
- Is it possible to print testID along side Component name on wdyr logs? HOT 1
- use jotai always diff
- dom npm
- wdyr not work in next13 (repo provided) HOT 2
- wdyr not work in next13 (repo provided)
- WDYR includes full lodash library with window._ override HOT 6
- Cannot get WDYR output from nextjs dynamically loaded component (e.g. for no-ssr use case) HOT 2
- Project still being actively maintained? HOT 2
- Multiple React roots
- why-did-you-render + React Query
- No logs or anything about WDYR HOT 9
- Missing `ownerDataMap` in `notifier`
- Partially Working with React 18 + NextJS 13
- "Support for defaultProps will be removed" error after setting include: [/./] option HOT 2
- Tracking custom hooks issue HOT 2
- WhyDidYouRender is not working in Storybook.js
- Property 'React' doesn't exist HOT 1
- react-refresh-webpack-plugin not updating component when wrapped with React.memo.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from why-did-you-render.