perf(TemplateBlockProvider): use performance hooks

This commit is contained in:
Zeke Zhang 2024-11-12 08:12:54 +08:00
parent 87b67bc460
commit 2976aebfc1

View File

@ -7,7 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import React, { createContext, useContext, useState } from 'react'; import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
const TemplateBlockContext = createContext<{ const TemplateBlockContext = createContext<{
// 模板是否已经请求结束 // 模板是否已经请求结束
@ -25,11 +25,9 @@ export const useTemplateBlockContext = () => {
const TemplateBlockProvider = (props) => { const TemplateBlockProvider = (props) => {
const [templateFinished, setTemplateFinished] = useState(false); const [templateFinished, setTemplateFinished] = useState(false);
return ( const onTemplateSuccess = useCallback(() => setTemplateFinished(true), []);
<TemplateBlockContext.Provider value={{ templateFinished, onTemplateSuccess: () => setTemplateFinished(true) }}> const value = useMemo(() => ({ templateFinished, onTemplateSuccess }), [onTemplateSuccess, templateFinished]);
{props.children} return <TemplateBlockContext.Provider value={value}>{props.children}</TemplateBlockContext.Provider>;
</TemplateBlockContext.Provider>
);
}; };
export { TemplateBlockProvider }; export { TemplateBlockProvider };