Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect exporter memoization in ListActions #6988

Closed
horaklukas opened this issue Dec 13, 2021 · 1 comment · Fixed by #6989
Closed

Incorrect exporter memoization in ListActions #6988

horaklukas opened this issue Dec 13, 2021 · 1 comment · Fixed by #6989
Labels

Comments

@horaklukas
Copy link

What you were expecting:
When I switch resource, I get correct list actions with export button

What happened instead:
When I switch resource, export button is missing because the result of ListActions component is memoized

Steps to reproduce:
I have two resource, both has list with export action.
I have a tiny wrapper around ListActions which render export action conditionally according to the user permissions which are loaded asynchronously.

  1. On load I can see export button in resource1
  2. When I switch to resource2 I can also see export button
  3. Switch back on resource1 cause export button to disappear
  4. When I switch to resource2, back button is also missing

Related code:

This is an excerpt from my code:

import { usePermissions } from 'react-admin';

export const useAppPermissions = () => usePermissions<RolePermissions>();

export const useHasAccess = (resource: ResourceKey, ...askedPermissions: PermissionAction[]) => {
    const { permissions } = usePermissions()

    return hasAccess(permissions, resource, ...askedPermissions);
};


/**
 * ListActions wrapper for conditional rendering of some actions according to the permissions
 */
import type { ListActionsProps } from 'react-admin';

export interface ListWithPermissionProps extends ListActionsProps {}

export const ListActionsWithPermissions = ({ filters, exporter, ...props }: ListWithPermissionProps) => {
    const resource = useResourceContext() as ResourceKey;
    const access = {
        search: useHasAccess(resource, PermissionAction.SEARCH),
        export: useHasAccess(resource, PermissionAction.EXPORT),
    };
    console.log('exporter', access.export ? exporter : false)
    return (
        <ListActions
            {...props}
            filters={access.search ? filters : undefined}
            exporter={access.export ? exporter : false}
        />
    );
};

Other information

I suppose I've also found the source of the issue. There is a missing exporter dependency in useMemo hook inside ListActions so even when the exporter value changes, the old value is used.

Environment

  • React-admin version: 3.18.1
  • React version: 17.0.2
  • Browser: Chrome
  • Stack trace (in case of a JS error):
@WiXSL WiXSL added the bug label Dec 13, 2021
@WiXSL
Copy link
Contributor

WiXSL commented Dec 13, 2021

Reproduced. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants