Use useMemo and useCallback to memoize context values
This commit is contained in:
committed by
Jack Kitterhing
parent
2f2a00cbbe
commit
c921bc6f71
@@ -1,5 +0,0 @@
|
|||||||
export default function getFeaturesContext(data) {
|
|
||||||
const flags = data.mailpoet_feature_flags;
|
|
||||||
const isSupported = (feature) => flags[feature] || false;
|
|
||||||
return { isSupported };
|
|
||||||
}
|
|
@@ -1,5 +0,0 @@
|
|||||||
export default function getSegmentsContext(data) {
|
|
||||||
return {
|
|
||||||
all: data.mailpoetSegments,
|
|
||||||
};
|
|
||||||
}
|
|
@@ -1,5 +0,0 @@
|
|||||||
export default function getUsersContext(data) {
|
|
||||||
return {
|
|
||||||
isNewUser: data.mailpoet_is_new_user,
|
|
||||||
};
|
|
||||||
}
|
|
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import getFeaturesContext from './getFeaturesContext.jsx';
|
import useFeaturesContext from './useFeaturesContext.jsx';
|
||||||
import getSegmentsContext from './getSegmentsContext.jsx';
|
import useSegmentsContext from './useSegmentsContext.jsx';
|
||||||
import getUsersContext from './getUsersContext.jsx';
|
import useUsersContext from './useUsersContext.jsx';
|
||||||
import useNotices from './useNotices.jsx';
|
import useNotices from './useNotices.jsx';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,9 +10,9 @@ import useNotices from './useNotices.jsx';
|
|||||||
* some React hooks to build some parts of the context.
|
* some React hooks to build some parts of the context.
|
||||||
*/
|
*/
|
||||||
export function useGlobalContextValue(data) {
|
export function useGlobalContextValue(data) {
|
||||||
const features = getFeaturesContext(data);
|
const features = useFeaturesContext(data);
|
||||||
const segments = getSegmentsContext(data);
|
const segments = useSegmentsContext(data);
|
||||||
const users = getUsersContext(data);
|
const users = useUsersContext(data);
|
||||||
const notices = useNotices();
|
const notices = useNotices();
|
||||||
return {
|
return {
|
||||||
features, segments, users, notices,
|
features, segments, users, notices,
|
||||||
|
9
assets/js/src/context/useFeaturesContext.jsx
Normal file
9
assets/js/src/context/useFeaturesContext.jsx
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export default function useFeaturesContext(data) {
|
||||||
|
return React.useMemo(() => {
|
||||||
|
const flags = data.mailpoet_feature_flags;
|
||||||
|
const isSupported = (feature) => flags[feature] || false;
|
||||||
|
return { isSupported };
|
||||||
|
}, [data]);
|
||||||
|
}
|
@@ -4,23 +4,35 @@ export default () => {
|
|||||||
const [items, setItems] = React.useState([]);
|
const [items, setItems] = React.useState([]);
|
||||||
const [nextId, setNextId] = React.useState(1);
|
const [nextId, setNextId] = React.useState(1);
|
||||||
|
|
||||||
const getNextId = () => {
|
const getNextId = React.useCallback(() => {
|
||||||
setNextId((x) => x + 1);
|
setNextId((x) => x + 1);
|
||||||
return nextId;
|
return nextId;
|
||||||
};
|
}, [nextId]);
|
||||||
|
|
||||||
const add = (item) => {
|
const add = React.useCallback((item) => {
|
||||||
setItems((xs) => [...xs, { ...item, id: item.id || getNextId() }]);
|
setItems((xs) => [...xs, { ...item, id: item.id || getNextId() }]);
|
||||||
};
|
}, [getNextId]);
|
||||||
|
|
||||||
const remove = (id) => {
|
const remove = React.useCallback((id) => {
|
||||||
setItems((xs) => xs.filter((x) => x.id !== id));
|
setItems((xs) => xs.filter((x) => x.id !== id));
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const success = (content, props = {}) => add({ ...props, type: 'success', children: content });
|
const success = React.useCallback(
|
||||||
const info = (content, props = {}) => add({ ...props, type: 'info', children: content });
|
(content, props = {}) => add({ ...props, type: 'success', children: content }),
|
||||||
const warning = (content, props = {}) => add({ ...props, type: 'warning', children: content });
|
[add]
|
||||||
const error = (content, props = {}) => add({ ...props, type: 'error', children: content });
|
);
|
||||||
|
const info = React.useCallback(
|
||||||
|
(content, props = {}) => add({ ...props, type: 'info', children: content }),
|
||||||
|
[add]
|
||||||
|
);
|
||||||
|
const warning = React.useCallback(
|
||||||
|
(content, props = {}) => add({ ...props, type: 'warning', children: content }),
|
||||||
|
[add]
|
||||||
|
);
|
||||||
|
const error = React.useCallback(
|
||||||
|
(content, props = {}) => add({ ...props, type: 'error', children: content }),
|
||||||
|
[add]
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
items, success, info, warning, error, remove,
|
items, success, info, warning, error, remove,
|
||||||
|
7
assets/js/src/context/useSegmentsContext.jsx
Normal file
7
assets/js/src/context/useSegmentsContext.jsx
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export default function useSegmentsContext(data) {
|
||||||
|
return React.useMemo(() => ({
|
||||||
|
all: data.mailpoetSegments,
|
||||||
|
}), [data]);
|
||||||
|
}
|
7
assets/js/src/context/useUsersContext.jsx
Normal file
7
assets/js/src/context/useUsersContext.jsx
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export default function useUsersContext(data) {
|
||||||
|
return React.useMemo(() => ({
|
||||||
|
isNewUser: data.mailpoet_is_new_user,
|
||||||
|
}), [data]);
|
||||||
|
}
|
@@ -7,10 +7,9 @@ import Notices from 'notices/notices.jsx';
|
|||||||
const ExperimentalFeatures = () => {
|
const ExperimentalFeatures = () => {
|
||||||
const [flags, setFlags] = useState(null);
|
const [flags, setFlags] = useState(null);
|
||||||
const contextValue = useGlobalContextValue(window);
|
const contextValue = useGlobalContextValue(window);
|
||||||
const [mounted, setMounted] = useState(false);
|
const showError = contextValue.notices.error;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!mounted) {
|
|
||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
api_version: window.mailpoet_api_version,
|
api_version: window.mailpoet_api_version,
|
||||||
endpoint: 'featureFlags',
|
endpoint: 'featureFlags',
|
||||||
@@ -20,15 +19,13 @@ const ExperimentalFeatures = () => {
|
|||||||
setFlags(flagsMap);
|
setFlags(flagsMap);
|
||||||
}).fail((response) => {
|
}).fail((response) => {
|
||||||
if (response.errors.length > 0) {
|
if (response.errors.length > 0) {
|
||||||
contextValue.notices.error(
|
showError(
|
||||||
<>{response.errors.map((error) => <p>{error.message}</p>)}</>,
|
<>{response.errors.map((error) => <p>{error.message}</p>)}</>,
|
||||||
{ scroll: true }
|
{ scroll: true }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setMounted(true);
|
}, [showError]);
|
||||||
}
|
|
||||||
}, [contextValue.notices, mounted]);
|
|
||||||
|
|
||||||
function handleChange(event) {
|
function handleChange(event) {
|
||||||
const name = event.target.name;
|
const name = event.target.name;
|
||||||
@@ -49,7 +46,7 @@ const ExperimentalFeatures = () => {
|
|||||||
contextValue.notices.success(<p>{message}</p>);
|
contextValue.notices.success(<p>{message}</p>);
|
||||||
}).fail((response) => {
|
}).fail((response) => {
|
||||||
if (response.errors.length > 0) {
|
if (response.errors.length > 0) {
|
||||||
contextValue.notices.error(
|
showError(
|
||||||
response.errors.map((error) => <p key={error.message}>{error.message}</p>),
|
response.errors.map((error) => <p key={error.message}>{error.message}</p>),
|
||||||
{ scroll: true }
|
{ scroll: true }
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user