Skip to content

Commit

Permalink
Merge pull request #2293 from mariusdkm/fix-use-memo
Browse files Browse the repository at this point in the history
Fix Dropdown useMemo not detecting equal objects
  • Loading branch information
T4rk1n authored Nov 2, 2022
2 parents 6921d34 + ae4de23 commit 0879395
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
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/[email protected]';
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

0 comments on commit 0879395

Please sign in to comment.