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

Tooltip rendering crashes when resizing the chart "Cannot read properties of undefined (reading 'payload')" #3981

Closed
1 task done
tran-simon opened this issue Nov 20, 2023 · 5 comments

Comments

@tran-simon
Copy link
Contributor

tran-simon commented Nov 20, 2023

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  1. Hover the chart to show the tooltip
  2. Press the button
  3. Resize the window (to show/hide the scrollbar)
  4. Repeat 3 times

The app crashes sometimes with "Cannot read properties of undefined (reading 'payload')" when the window is resized.

The crash is not consistent, so you should reload and try multiple times to reproduce

What is expected?

The tooltip should appear correctly

What is actually happening?

The whole app crashes (null pointer exception)

Environment Info
Recharts v2.10.1
React 18.2.0
System codesandbox
Browser Chrome

This is due to a mistake in getItemByXY in generateCategoricalChart.tsx.

isFunnel(graphicalItem, activeItem) ||
isPie(graphicalItem, activeItem) ||
isScatter(graphicalItem, activeItem)

export function isFunnel(graphicalItem: GraphicalItem, _item: unknown): _item is FunnelItem {
return 'trapezoids' in graphicalItem.props;
}
export function isPie(graphicalItem: GraphicalItem, _item: unknown): _item is PieItem {
return 'sectors' in graphicalItem.props;
}
export function isScatter(graphicalItem: GraphicalItem, _item: unknown): _item is ScatterItem {
return 'points' in graphicalItem.props;
}

isFunnel, isPie and isScatter do not check if activeItem is defined, so it might cast it as a defined FunnelItem/PieItem/ScatterItem even if activeItem is undefined

@ckifer
Copy link
Member

ckifer commented Nov 20, 2023

Thanks for the fix. I can't reproduce at all, I wonder why its so random

Edit: have retried the reproduction steps on different browsers multiple times and still can't reproduce

@tran-simon
Copy link
Contributor Author

@ckifer yeah it's really hard to reproduce, I'll keep looking for a way to reproduce it consistently

But I still think that part of the code is smelly, activeItem is typed as optional but the isFunnel/is... functions cast it as defined without checking if it actually is

@ckifer
Copy link
Member

ckifer commented Nov 20, 2023

makes sense

ckifer pushed a commit that referenced this issue Nov 20, 2023
<!--- Provide a general summary of your changes in the Title above -->

## Description

Sometimes, the tooltip rendering would crash when the window has been
resized

> TypeError: Cannot read properties of undefined (reading 'payload')


This is due to a mistake in `getItemByXY` in
`generateCategoricalChart.tsx`.

https://github.com/recharts/recharts/blob/0d5326a8cdcad34a3b9d7c0644abbb645ddffe87/src/chart/generateCategoricalChart.tsx#L2250-L2252


https://github.com/recharts/recharts/blob/7fb227dae542c3d3093506e6d80a2c2c366f9a26/src/util/ActiveShapeUtils.tsx#L142-L152

`isFunnel`, `isPie` and `isScatter` do not check if `activeItem` is
defined, so it might cast it as a defined
`FunnelItem`/`PieItem`/`ScatterItem` even if `activeItem` is undefined

<!--- Describe your changes in detail -->

I've added a nullcheck for activeItem

## Related Issue

#3981

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

This fixes a crash

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

I manually checked that the crash doesn't happen anymore

## Screenshots (if appropriate):

<img width="1040" alt="image"
src="https://github.com/recharts/recharts/assets/8755930/9c5eaf0d-0bf4-486d-94fb-ae77b0ba8878">


## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] My code follows the code style of this project.
- [x] My change requires a change to the documentation.
- [x] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] I have added a storybook story or extended an existing story to
show my changes
- [x] All new and existing tests passed.
@agahakan
Copy link

A similar issue can be consistently reproduced in this sandbox.

Hover your mouse over the chart and wait 3 seconds; an error will be thrown: 'Cannot read properties of undefined (reading 'payload')'.

error is caused by the useIncrement hook, despite count not being used, its presence triggers the error.
to avoid this error you can just move the data array outside of the App component. or you can comment count.

these are not ideal solutions since data might need to be dependent on a hook

GMer78 pushed a commit to GMer78/recharts-1 that referenced this issue Nov 24, 2023
<!--- Provide a general summary of your changes in the Title above -->

## Description

Sometimes, the tooltip rendering would crash when the window has been
resized

> TypeError: Cannot read properties of undefined (reading 'payload')


This is due to a mistake in `getItemByXY` in
`generateCategoricalChart.tsx`.

https://github.com/recharts/recharts/blob/0d5326a8cdcad34a3b9d7c0644abbb645ddffe87/src/chart/generateCategoricalChart.tsx#L2250-L2252


https://github.com/recharts/recharts/blob/7fb227dae542c3d3093506e6d80a2c2c366f9a26/src/util/ActiveShapeUtils.tsx#L142-L152

`isFunnel`, `isPie` and `isScatter` do not check if `activeItem` is
defined, so it might cast it as a defined
`FunnelItem`/`PieItem`/`ScatterItem` even if `activeItem` is undefined

<!--- Describe your changes in detail -->

I've added a nullcheck for activeItem

## Related Issue

recharts#3981

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

This fixes a crash

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

I manually checked that the crash doesn't happen anymore

## Screenshots (if appropriate):

<img width="1040" alt="image"
src="https://github.com/recharts/recharts/assets/8755930/9c5eaf0d-0bf4-486d-94fb-ae77b0ba8878">


## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] My code follows the code style of this project.
- [x] My change requires a change to the documentation.
- [x] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] I have added a storybook story or extended an existing story to
show my changes
- [x] All new and existing tests passed.
@ckifer
Copy link
Member

ckifer commented Nov 29, 2023

fixed merged and released in 2.10.2 - please comment back if this persists

@ckifer ckifer closed this as completed Nov 29, 2023
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

3 participants