Don't call query (useState via fn) (#4569)

This commit is contained in:
Jaco Greeff 2021-02-01 20:36:55 +01:00 committed by GitHub
parent cd6980d1ce
commit 59ad14c658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,31 +30,27 @@ function InputStorage ({ className = '', defaultValue, help, label, onChange, wi
const { api } = useApi();
const [optionsMethod, setOptionsMethod] = useState<DropdownOptions>(() => keyOptions(api, defaultValue.creator.section));
const [optionsSection] = useState<DropdownOptions>(() => sectionOptions(api));
const [value, setValue] = useState<QueryableStorageEntry<'promise'>>(defaultValue);
const [value, setValue] = useState<QueryableStorageEntry<'promise'>>(() => defaultValue);
const _onKeyChange = useCallback(
(newValue: QueryableStorageEntry<'promise'>): void => {
if (value.creator.section === newValue.creator.section && value.creator.method === newValue.creator.method) {
return;
if (value.creator.section !== newValue.creator.section || value.creator.method !== newValue.creator.method) {
// set via callback
setValue(() => newValue);
onChange && onChange(newValue);
}
// set via callback
setValue(() => newValue);
onChange && onChange(newValue);
},
[onChange, value]
);
const _onSectionChange = useCallback(
(section: string): void => {
if (section === value.creator.section) {
return;
if (section !== value.creator.section) {
const optionsMethod = keyOptions(api, section);
setOptionsMethod(optionsMethod);
_onKeyChange(api.query[section][optionsMethod[0].value] as any);
}
const optionsMethod = keyOptions(api, section);
setOptionsMethod(optionsMethod);
_onKeyChange(api.query[section][optionsMethod[0].value] as any);
},
[_onKeyChange, api, value]
);