Skip to content

Commit a2be036

Browse files
refactor: rename useResource to useResourceParams (#6953)
1 parent a1be61d commit a2be036

File tree

75 files changed

+3322
-298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+3322
-298
lines changed

documentation/docs/core/refine-component/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const App = () => (
9696

9797
:::simple Accessing the Resource
9898

99-
You can use [useResource](/docs/routing/hooks/use-resource) hook to get the current active resource by the route or you can pass the `name` or the `identifier` of a resource to the `useResource` hook to get the resource object.
99+
You can use [useResourceParams](/docs/routing/hooks/use-resource-params) hook to get the current active resource by the route or you can pass the `name` or the `identifier` of a resource to the `useResourceParams` hook to get the resource object.
100100

101101
:::
102102

@@ -212,7 +212,7 @@ There's also a third option, which is to pass an object with the `component` and
212212

213213
### meta
214214

215-
`meta` can have any kind of property. It is used to store additional information about the resource. This property you pass can be received from the [useResource](/docs/routing/hooks/use-resource). Listed below are the properties that are used by Refine or its libraries.
215+
`meta` can have any kind of property. It is used to store additional information about the resource. This property you pass can be received from the [useResourceParams](/docs/routing/hooks/use-resource-params). Listed below are the properties that are used by Refine or its libraries.
216216

217217
#### label
218218

documentation/docs/migration-guide/4x-to-5x.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ Resource `options` have been renamed to `meta`:
416416

417417
#### resourceName/resourceNameOrRouteName → resource
418418

419-
🚨 Affects: useImport, useExport, useResource, All Button components
419+
🚨 Affects: useImport, useExport, All Button components
420420

421421
```diff
422422
useImport({
@@ -588,26 +588,19 @@ const MyComponent = () => {
588588
};
589589
```
590590

591-
#### useResource → useResourceParams (Recommended)
591+
#### useResource → useResourceParams
592592

593-
```diff
594-
// useResource parameter simplification
595-
- useResource({ resourceNameOrRouteName: "posts" });
596-
+ useResource("posts");
597-
```
593+
The `useResource` hook has been removed in favor of `useResourceParams`. The new `useResourceParams` hook offers the same functionality as `useResource`, while introducing additional features and a more streamlined API. To reduce confusion and improve consistency, all resource-related logic should now use `useResourceParams` exclusively.
598594

599-
:::tip Recommendation
600-
While `useResource` continues to work for backward compatibility, we now recommend using [`useResourceParams`](https://refine.dev/docs/routing/hooks/use-resource-params/) for new projects. It provides a more stable, granular, and precise solution for resource management.
595+
```diff
596+
- import { useResource } from "@refinedev/core";
597+
+ import { useResourceParams } from "@refinedev/core";
601598

602-
```typescript
603-
// Recommended approach
604-
import { useResourceParams } from "@refinedev/core";
605599

606-
const { resource, action, id } = useResourceParams();
600+
- useResource("posts");
601+
+ useResourceParams({ resource: "posts" });
607602
```
608603

609-
:::
610-
611604
#### ignoreAccessControlProvider → accessControl
612605

613606
```diff

documentation/docs/routing/hooks/use-resource-params/index.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ title: useResourceParams
33
source: packages/core/src/hooks/use-resource-params
44
---
55

6-
`useResourceParams` is used to get the related parameters of the current resource such as `resource`, `id` and `action`. It also provides `formAction` to determine the action of the form and `setId` to set the `id` programmatically without having to use a separate state for it.
6+
`useResourceParams` is used to access the related parameters of the current resource, such as `resource`, `id`, and `action`. It also provides `formAction` to determine the form action, and `setId` to set the `id` programmatically without needing a separate state. In addition, it returns the `resources` array defined in `<Refine />`.
7+
8+
If you pass a resource name or identifier to `useResourceParams`, it will return the matching `resource` object. If no match is found, a temporary `resource` will be created using the provided name or identifier.
79

810
## Usage
911

@@ -68,6 +70,18 @@ Current action to be performed. This can be explicitly passed via the `action` p
6870

6971
Apart from the `action` value, `formAction` can only be `create`, `edit` or `clone`. If the `action` is not one of these, `formAction` will be set to `create` for convenience.
7072

73+
### resources
74+
75+
An array of resources that you defined in `<Refine>`.
76+
77+
### select
78+
79+
The function allows you to retrieve a `resource` object and matched `identifier` by providing either a resource `name` or `identifier`. By default, if there is no match for the given `name` or `identifier`, the function will return the `resource` object and `identifier` associated with the provided value.
80+
81+
If you don't pass any parameter to `useResource`, it will try to infer the `resource` from the current route. If there is no match, the `resource` and `identifier` will be `undefined`.
82+
83+
The function also accepts a second parameter `force` which is `true` by default. If you set it to `false`, it will not return a `resource` object and `identifier` if there is no match.
84+
7185
## API Reference
7286

7387
### Properties
@@ -76,11 +90,13 @@ Apart from the `action` value, `formAction` can only be `create`, `edit` or `clo
7690

7791
### Return value
7892

79-
| Description | Type |
80-
| ----------- | -------------------------------------------------------------------------- |
81-
| resource | `IResourceItem` \| `undefined` |
82-
| identifier | `string` \| `undefined` |
83-
| id | [`BaseKey` \| `undefined`](/docs/core/interface-references#basekey) |
84-
| setId | `(id: BaseKey) => void` |
85-
| action | `undefined` \| `"list"` \| `"create"` \| `"edit"` \| `"show"` \| `"clone"` |
86-
| formAction | `"create"` \| `"edit"` \| `"clone"` |
93+
| Description | Type |
94+
| ----------- | ------------------------------------------------------------------------------------------------------------------------- |
95+
| resource | `IResourceItem` \| `undefined` |
96+
| identifier | `string` \| `undefined` |
97+
| id | [`BaseKey` \| `undefined`](/docs/core/interface-references#basekey) |
98+
| setId | `(id: BaseKey) => void` |
99+
| action | `undefined` \| `"list"` \| `"create"` \| `"edit"` \| `"show"` \| `"clone"` |
100+
| formAction | `"create"` \| `"edit"` \| `"clone"` |
101+
| select | `(resourceName: string, force?: boolean) => { resource: IResourceItem` \| `undefined, identifier: string` \| `undefined}` |
102+
| resources | [`IResourceItem[]`](#interfaces) |

documentation/docs/routing/hooks/use-resource/index.md

Lines changed: 0 additions & 102 deletions
This file was deleted.

documentation/redirects.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,15 @@
154154
},
155155
{
156156
"from": "/docs/core/hooks/resource/useResource/",
157-
"to": "/docs/routing/hooks/use-resource"
157+
"to": "/docs/routing/hooks/use-resource-params"
158158
},
159159
{
160160
"from": "/docs/core/hooks/resource/useResourceWithRoute/",
161-
"to": "/docs/routing/hooks/use-resource"
161+
"to": "/docs/routing/hooks/use-resource-params"
162+
},
163+
{
164+
"from": "/docs/routing/hooks/use-resource",
165+
"to": "/docs/routing/hooks/use-resource-params"
162166
},
163167
{
164168
"from": "/docs/core/hooks/show/useShow/",
@@ -674,7 +678,7 @@
674678
},
675679
{
676680
"from": "/docs/api-reference/core/hooks/resource/useResourceWithRoute/",
677-
"to": "/docs/routing/hooks/use-resource"
681+
"to": "/docs/routing/hooks/use-resource-params"
678682
},
679683
{
680684
"from": "/docs/api-reference/core/hooks/refine/useTitle/",
@@ -1067,7 +1071,7 @@
10671071
},
10681072
{
10691073
"from": "/docs/api-reference/core/hooks/resource/useResource",
1070-
"to": "/docs/routing/hooks/use-resource"
1074+
"to": "/docs/routing/hooks/use-resource-params"
10711075
},
10721076
{
10731077
"from": "/docs/api-reference/core/hooks/show/useShow",
@@ -1915,7 +1919,7 @@
19151919
},
19161920
{
19171921
"from": "/docs/core/hooks/navigation/use-resource",
1918-
"to": "/docs/routing/hooks/use-resource"
1922+
"to": "/docs/routing/hooks/use-resource-params"
19191923
},
19201924
{
19211925
"from": "/docs/core/hooks/navigation/use-go",

documentation/sidebars.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ module.exports = {
244244
label: "Hooks",
245245
items: [
246246
"routing/hooks/use-resource-params/index",
247-
"routing/hooks/use-resource/index",
248247
"routing/hooks/use-go/index",
249248
"routing/hooks/use-back/index",
250249
"routing/hooks/use-parsed/index",

documentation/src/refine-theme/landing-hero-showcase-section.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ const ShowcaseECommerce = ({ className }: { className?: string }) => {
605605
"https://refine.ams3.cdn.digitaloceanspaces.com/website/static/showcase-images/finefoods/actions-2.png",
606606
codePosition: "left",
607607
code: `
608-
import { useResource, useUpdate } from "@refinedev/core";
608+
import { useResourceParams, useUpdate } from "@refinedev/core";
609609
610-
const { id } = useResource();
610+
const { id } = useResourceParams();
611611
const { mutate } = useUpdate();
612612
613613
const onReject = () => mutate({
@@ -706,9 +706,9 @@ const ShowcaseDevOps = ({ className }: { className?: string }) => {
706706
"https://refine.ams3.cdn.digitaloceanspaces.com/website/static/showcase-images/devops/actions.png",
707707
codePosition: "left",
708708
code: `
709-
import { useDelete, useResource } from "@refinedev/core";
709+
import { useDelete, useResourceParams } from "@refinedev/core";
710710
711-
const { id } = useResource();
711+
const { id } = useResourceParams();
712712
const { mutate } = useDelete();
713713
714714
const onDelete = () => {

examples/blog-refine-shadcn/src/pages/blog-posts/show.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
type IResourceComponentsProps,
44
useNavigation,
55
useOne,
6-
useResource,
6+
useResourceParams,
77
useShow,
88
} from "@refinedev/core";
99

@@ -12,7 +12,7 @@ import { ChevronLeft, EditIcon, ListIcon } from "lucide-react";
1212

1313
export const BlogPostShow: React.FC<IResourceComponentsProps> = () => {
1414
const { edit, list } = useNavigation();
15-
const { id } = useResource();
15+
const { id } = useResourceParams();
1616
const { query: queryResult } = useShow({
1717
meta: {
1818
populate: ["category"],

examples/blog-refine-shadcn/src/pages/categories/show.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { Button } from "@/components/ui/button";
22
import {
33
type IResourceComponentsProps,
44
useNavigation,
5-
useResource,
5+
useResourceParams,
66
useShow,
77
} from "@refinedev/core";
88
import { ChevronLeft, EditIcon, ListIcon } from "lucide-react";
99
import React from "react";
1010

1111
export const CategoryShow: React.FC<IResourceComponentsProps> = () => {
1212
const { edit, list } = useNavigation();
13-
const { id } = useResource();
13+
const { id } = useResourceParams();
1414
const { query: queryResult } = useShow({});
1515
const { data } = queryResult;
1616

examples/refine-hr-ce/src/components/layout/page-header/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { useResource } from "@refinedev/core";
2+
import { useResourceParams } from "@refinedev/core";
33
import { ListButton } from "@refinedev/mui";
44
import { Box, Divider, Typography } from "@mui/material";
55
import { ChevronLeftRectangleIcon } from "@/icons";
@@ -17,7 +17,7 @@ export const PageHeader = ({
1717
showListButton,
1818
showDivider,
1919
}: Props) => {
20-
const { resource } = useResource();
20+
const { resource } = useResourceParams();
2121

2222
const prefferedTitle = title || resource?.meta?.label || "";
2323

0 commit comments

Comments
 (0)