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

Fix Dropdown useMemo not detecting equal objects #2293

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#2292](https://github.com/plotly/dash/pull/2292) Pages: find the 404 page even if `pages_folder` is nested, or the 404 page is nested inside `pages_folder`.
- [#2265](https://github.com/plotly/dash/pull/2265) Removed deprecated `before_first_request` as reported in [#2177](https://github.com/plotly/dash/issues/2177).
- [#2257](https://github.com/plotly/dash/pull/2257) Fix tuple types in the TypeScript component generator.
- [#2293](https://github.com/plotly/dash/pull/2293) Fix Dropdown useMemo not detecting equal objects

### Changed

Expand Down
11 changes: 11 additions & 0 deletions components/dash-core-components/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/dash-core-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"react-dates": "^21.8.0",
"react-docgen": "^5.4.3",
"react-dropzone": "^4.1.2",
"react-fast-compare": "^3.2.0",
"react-markdown": "^4.3.1",
"react-resize-detector": "^6.7.6",
"react-select-fast-filter-options": "^0.2.3",
Expand Down
13 changes: 10 additions & 3 deletions components/dash-core-components/src/fragments/Dropdown.react.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isNil, pluck, without, pick} from 'ramda';
import React, {useState, useCallback, useEffect, useMemo} from 'react';
import React, {useState, useCallback, useEffect, useMemo, useRef} from 'react';
import ReactDropdown from 'react-virtualized-select';
import createFilterOptions from 'react-select-fast-filter-options';
import '../components/css/react-virtualized-select@3.1.0.css';
Expand All @@ -8,6 +8,7 @@ import '../components/css/Dropdown.css';

import {propTypes, defaultProps} from '../components/Dropdown.react';
import {sanitizeOptions} from '../utils/optionTypes';
import isEqual from 'react-fast-compare';

// Custom tokenizer, see https://github.com/bvaughn/js-search/issues/43
// Split on spaces
Expand Down Expand Up @@ -47,6 +48,12 @@ const Dropdown = props => {
value,
} = props;
const [optionsCheck, setOptionsCheck] = useState(null);
const persistentOptions = useRef(null);

if (!persistentOptions || !isEqual(options, persistentOptions.current)) {
persistentOptions.current = options;
}

const [sanitizedOptions, filterOptions] = useMemo(() => {
let sanitized = sanitizeOptions(options);

Expand Down Expand Up @@ -83,7 +90,7 @@ const Dropdown = props => {
indexes,
}),
];
}, [options]);
}, [persistentOptions.current]);

const onChange = useCallback(
selectedOption => {
Expand Down Expand Up @@ -146,7 +153,7 @@ const Dropdown = props => {
>
<ReactDropdown
filterOptions={filterOptions}
options={sanitizeOptions(options)}
options={sanitizedOptions}
value={value}
onChange={onChange}
onInputChange={onInputChange}
Expand Down