Use useMemo and useCallback to memoize context values

This commit is contained in:
Amine Ben hammou
2020-02-20 04:02:53 +01:00
committed by Jack Kitterhing
parent 2f2a00cbbe
commit c921bc6f71
9 changed files with 69 additions and 52 deletions

View File

@@ -7,28 +7,25 @@ import Notices from 'notices/notices.jsx';
const ExperimentalFeatures = () => {
const [flags, setFlags] = useState(null);
const contextValue = useGlobalContextValue(window);
const [mounted, setMounted] = useState(false);
const showError = contextValue.notices.error;
useEffect(() => {
if (!mounted) {
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'featureFlags',
action: 'getAll',
}).done((response) => {
const flagsMap = response.data.reduce((obj, item) => ({ ...obj, [item.name]: item }), {});
setFlags(flagsMap);
}).fail((response) => {
if (response.errors.length > 0) {
contextValue.notices.error(
<>{response.errors.map((error) => <p>{error.message}</p>)}</>,
{ scroll: true }
);
}
});
setMounted(true);
}
}, [contextValue.notices, mounted]);
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'featureFlags',
action: 'getAll',
}).done((response) => {
const flagsMap = response.data.reduce((obj, item) => ({ ...obj, [item.name]: item }), {});
setFlags(flagsMap);
}).fail((response) => {
if (response.errors.length > 0) {
showError(
<>{response.errors.map((error) => <p>{error.message}</p>)}</>,
{ scroll: true }
);
}
});
}, [showError]);
function handleChange(event) {
const name = event.target.name;
@@ -49,7 +46,7 @@ const ExperimentalFeatures = () => {
contextValue.notices.success(<p>{message}</p>);
}).fail((response) => {
if (response.errors.length > 0) {
contextValue.notices.error(
showError(
response.errors.map((error) => <p key={error.message}>{error.message}</p>),
{ scroll: true }
);