Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing ownerDataMap in notifier #281

Open
zeorin opened this issue Oct 17, 2023 · 0 comments
Open

Missing ownerDataMap in notifier #281

zeorin opened this issue Oct 17, 2023 · 0 comments

Comments

@zeorin
Copy link

zeorin commented Oct 17, 2023

When providing a getAdditionalOwnerData function in the options, there is no ownerDataMap provided to the notifier.

Based on a reading of the source, it also seems to me that passing ownerDataMap to the notifier is probably not what was meant, but that it should probably be passed the additionalOwnerData instead, given that ownerDataMap is accessible at whyDidYouRender.wdyrStore.ownerDataMap anyway. additionalOwnerData is also not passed to notifier.

At runtime, the additionalOwnerData is actually added to the values in wdyrStore.ownerDataMap, but nothing is done with them.

Additionally, if one tries to get the additionalOwnerData oneself inside the notifier by retrieving it from the whyDidYouRender.wdyrStore.ownerDataMap using either the prevProps or the nextProps, this doesn't work, as the props that are used for the keys are the element's props, and not the owner's props. It is the owner's props that are passed to the notifier.

I was able to work around this by creating my own mapping between owner props and element props, so that I could lookup the element props to use as the key to the ownerDataMap:

import React from 'react';
import whyDidYouRender from '@welldone-software/why-did-you-render';

const { defaultNotifier, wdyrStore } = whyDidYouRender;

const elementOwnerPropsMap = new WeakMap();

whyDidYouRender(React, {
  getAdditionalOwnerData: (element) => {
    const owner = wdyrStore.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current; // is this the same as `element._owner`?
    elementOwnerPropsMap.set(owner.pendingProps, element.props);
    return { owner };
  },
  notifier: (updateInfo) => {
    const { additionalOwnerData } = wdyrStore.ownerDataMap.get(
      elementOwnerPropsMap.get(updateInfo.prevProps) // `updateInfo.nextProps` never matches anything
    ) ?? {};

    // do stuff with `additionalOwnerData`…

    defaultNotifier(updateInfo);
  },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant