mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 23:26:38 +00:00
perf(useResourceName): avoid unnecessary re-renders
This commit is contained in:
parent
617f9b8563
commit
063c2f7160
@ -14,11 +14,15 @@ import React, { createContext, useCallback, useContext, useEffect, useMemo } fro
|
||||
import { Navigate } from 'react-router-dom';
|
||||
import { useAPIClient, useRequest } from '../api-client';
|
||||
import { useAppSpin } from '../application/hooks/useAppSpin';
|
||||
import { useBlockRequestContext } from '../block-provider/BlockProvider';
|
||||
import { useResourceActionContext } from '../collection-manager/ResourceActionProvider';
|
||||
import { CollectionNotAllowViewPlaceholder, useCollection, useCollectionManager } from '../data-source';
|
||||
import {
|
||||
CollectionNotAllowViewPlaceholder,
|
||||
useCollection,
|
||||
useCollectionManager,
|
||||
useCollectionRecordData,
|
||||
useDataBlockProps,
|
||||
} from '../data-source';
|
||||
import { useDataSourceKey } from '../data-source/data-source/DataSourceProvider';
|
||||
import { useRecord } from '../record-provider';
|
||||
import { SchemaComponentOptions, useDesignable } from '../schema-component';
|
||||
|
||||
import { useApp } from '../application';
|
||||
@ -177,12 +181,12 @@ const useAllowedActions = () => {
|
||||
|
||||
const useResourceName = () => {
|
||||
const service = useResourceActionContext();
|
||||
const result = useBlockRequestContext() || { service };
|
||||
const dataBlockProps = useDataBlockProps();
|
||||
return (
|
||||
result?.props?.resource ||
|
||||
result?.props?.association ||
|
||||
result?.props?.collection ||
|
||||
result?.service?.defaultRequest?.resource
|
||||
dataBlockProps?.resource ||
|
||||
dataBlockProps?.association ||
|
||||
dataBlockProps?.collection ||
|
||||
service?.defaultRequest?.resource
|
||||
);
|
||||
};
|
||||
|
||||
@ -282,14 +286,14 @@ export const useACLActionParamsContext = () => {
|
||||
|
||||
export const useRecordPkValue = () => {
|
||||
const collection = useCollection();
|
||||
const record = useRecord();
|
||||
const recordData = useCollectionRecordData();
|
||||
|
||||
if (!collection) {
|
||||
return;
|
||||
}
|
||||
|
||||
const primaryKey = collection.getPrimaryKey();
|
||||
return record?.[primaryKey];
|
||||
return recordData?.[primaryKey];
|
||||
};
|
||||
|
||||
export const ACLActionProvider = (props) => {
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
import { useField } from '@formily/react';
|
||||
import { Result } from 'ahooks/es/useRequest/src/types';
|
||||
import React, { createContext, useContext, useEffect } from 'react';
|
||||
import React, { createContext, useContext, useEffect, useMemo } from 'react';
|
||||
import { useCollectionManager_deprecated } from '.';
|
||||
import { CollectionProvider_deprecated, useRecord } from '..';
|
||||
import { useAPIClient, useRequest } from '../api-client';
|
||||
@ -58,9 +58,15 @@ const CollectionResourceActionProvider = (props) => {
|
||||
{ uid },
|
||||
);
|
||||
const resource = api.resource(request.resource);
|
||||
const resourceActionValue = useMemo(
|
||||
() => ({ ...service, defaultRequest: request, dragSort }),
|
||||
[dragSort, request, service],
|
||||
);
|
||||
const resourceContextValue = useMemo(() => ({ type: 'collection', resource, collection }), [collection, resource]);
|
||||
|
||||
return (
|
||||
<ResourceContext.Provider value={{ type: 'collection', resource, collection }}>
|
||||
<ResourceActionContext.Provider value={{ ...service, defaultRequest: request, dragSort }}>
|
||||
<ResourceContext.Provider value={resourceContextValue}>
|
||||
<ResourceActionContext.Provider value={resourceActionValue}>
|
||||
<CollectionProvider_deprecated collection={collection}>{props.children}</CollectionProvider_deprecated>
|
||||
</ResourceActionContext.Provider>
|
||||
</ResourceContext.Provider>
|
||||
@ -88,9 +94,18 @@ const AssociationResourceActionProvider = (props) => {
|
||||
{ uid },
|
||||
);
|
||||
const resource = api.resource(request.resource, resourceOf);
|
||||
const resourceContextValue = useMemo(
|
||||
() => ({ type: 'association', resource, association, collection }),
|
||||
[association, collection, resource],
|
||||
);
|
||||
const resourceActionContextValue = useMemo(
|
||||
() => ({ ...service, defaultRequest: request, dragSort }),
|
||||
[dragSort, request, service],
|
||||
);
|
||||
|
||||
return (
|
||||
<ResourceContext.Provider value={{ type: 'association', resource, association, collection }}>
|
||||
<ResourceActionContext.Provider value={{ ...service, defaultRequest: request, dragSort }}>
|
||||
<ResourceContext.Provider value={resourceContextValue}>
|
||||
<ResourceActionContext.Provider value={resourceActionContextValue}>
|
||||
<CollectionProvider_deprecated collection={collection}>{props.children}</CollectionProvider_deprecated>
|
||||
</ResourceActionContext.Provider>
|
||||
</ResourceContext.Provider>
|
||||
|
Loading…
Reference in New Issue
Block a user