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

useRegisterResource is not exported #7623

Closed
panfiva opened this issue May 3, 2022 · 8 comments
Closed

useRegisterResource is not exported #7623

panfiva opened this issue May 3, 2022 · 8 comments

Comments

@panfiva
Copy link

panfiva commented May 3, 2022

What you were expecting:
useRegisterResource should be exported in react-admin

What happened instead:
useRegisterResource is no longer exported in 4.0.2

Recevied error when importing useRegisterResource

TS2305: Module '"react-admin"' has no exported member 'useRegisterResource'.

Environment

  • React-admin version: 4.0.2
  • Last version that did not exhibit the issue (if applicable): 4.0.1
  • React version:
  • Browser:
  • Stack trace (in case of a JS error):
@panfiva panfiva changed the title useRegister useRegisterResource is not exported May 3, 2022
@fzaninotto
Copy link
Member

Why did you use this hook? As far as I know, it was not documented anywhere - to put it otherwise, a private API.

@panfiva
Copy link
Author

panfiva commented May 3, 2022

We se it to dynamically register resources so that all components of List and Showwork properly.

For example, we have a page for that displays AD account records and is routable via /ad_accounts endpoint.

Inside /ad_accounts Show, we display device metadata in a tab, and also has another tab that displays account history (ad_accounts_log resource that is not registered as resource under <Admin>)

Since it is not registered, some RA features that rely on "hasShow" or "hasList" do not work properly. For example, "ShowButton" does not work.

@panfiva
Copy link
Author

panfiva commented May 3, 2022

Here is a component that we created that we use in custom routes, so that RA features work properly.

/**
 * This component will register resource in resource ResourceDefinitionContext in React-Admin
 * If a resource is not registered in ResourceDefinitionContext, some generic functions in List, Create, Edit and Show will not work properly
 * For example, Create button will be missing on List component
 *
 * @example
 * <ResourceWithReg name='users' list={UserList} />
 */
export function ResourceWithReg(props: ResourceProps) {
	const def: ResourceDefinitions = useResourceDefinitions()

	const { name, create, edit, list, show, icon } = props

	const hasList = !!list
	const hasCreate = !!create
	const hasEdit = !!edit
	const hasShow = !!show

	const registerResource = useRegisterResource()

	const isRegistered = !!def[props.name]

	useEffect(() => {
		if (!isRegistered) {
			console.debug(`Registering "${name}" resource`)
			registerResource({ name, hasList, hasCreate, hasEdit, hasShow, icon })
		}
	}, [registerResource, isRegistered, name, hasList, hasCreate, hasEdit, hasShow, icon])

	if (isRegistered) return <Resource {...props} />
	else return null
}

export default ResourceWithReg

@panfiva
Copy link
Author

panfiva commented May 3, 2022

An example of a page that we render under /ad_accounts/9513/show/ad_accounts_log/27075/show

Change Log table is a List from ad_accounts_log resource that is related to AD account ad_accounts id=9513;

Additional details are a record ad_accounts_log = 27075

To make all features work properly, before we render ad_accounts_log , we modify basePath from "" to "/ad_accounts/9513/show/"; after that, all React-Admin components work well, as long as resource is registered

image

@panfiva
Copy link
Author

panfiva commented May 3, 2022

An alternative solution is to update React-Admin's ` component to check if resource is registered, and register the resource before returning child components.

@smeng9
Copy link
Contributor

smeng9 commented May 3, 2022

@panfiva Here is the linked pull request, and the use case is mentioned there, #7455

You can still use the const { register: registerResource } = useResourceDefinitionContext(); to register.

I also noticed the method is no longer exported when upgrading to 4.0.2. If we still want to support this use case we can add an example in documentation.

@panfiva
Copy link
Author

panfiva commented May 3, 2022

@fzaninotto,

I would like to have an ability to access registerResource function, either through useResourceDefinitionContext() or 'useRegisterResource()'. If we could make one of these method exportable and supportable in the future, that would be great.

as an alternative, can we modify <Resource> component to register resources if they are not yet registered via Admin component.

thanks

@fzaninotto
Copy link
Member

So the useRegisterResource hook was refactored in #7539, and became useResourceDefinitionContext. The syntax has changed, but it is still exported. You will need to update your code to make it work with 4.0.2 and further releases.

We still consider this hook internal API though, so we won't document it or support its usage outside of react-admin modules. The official way of adding resource at runtime is documented at https://marmelab.com/react-admin/Admin.html#declaring-resources-at-runtime.

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

No branches or pull requests

3 participants