Usage with Existing Projects
Integrating Refine into an existing project is as simple as installing @refinedev/core package and importing Refine component into your application.
Once imported, Refine component provides necessary context to all children components for Refine hooks and components to work.
Only required prop for Refine component is dataProvider. You can read more about data provider here.
Quickstart
Only needed package for Refine to work is @refinedev/core. For demonstration purposes, we will also install @refinedev/simple-rest package to use as data provider. You can use one of our data providers or create your own.
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/simple-rest
yarn add @refinedev/core @refinedev/simple-rest
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
const API_URL = "https://api.fake-rest.refine.dev";
function App() {
return (
<ExistingProvider>
<Refine dataProvider={dataProvider(API_URL)}>
{/* You can use Refine hooks inside here */}
<ComponentWithRefineHooks />
<ExistingComponent1>
</Refine>
</ExistingProvider>
);
}
Headless Examples
The following example shows how to use Refine's useShow hook with an existing application.
- Vite
- Next.js App
- Next.js Pages
Vite
As you can see in the example below, wrapping App.tsx file with Refine component is enough to use Refine hooks inside your application.
Sandpack component is temporarily disabled for compatibility reasons.
Next.js App
As you can see in the example below, wrapping layout.tsx file with Refine component is enough to use Refine hooks & components inside your application.
Sandpack component is temporarily disabled for compatibility reasons.
Next.js Pages
As you can see in the example below, wrapping _app.tsx file with Refine component is enough to use Refine hooks & components inside your application.
Sandpack component is temporarily disabled for compatibility reasons.
Router Examples
In the following examples below, we will integrate Refine into /refine route of an existing application.
See the Routing Guide for more information.
- React Router
- Next.js App
- Next.js Pages
React Router
First, we need to install necessary packages:
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/react-router @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/react-router @refinedev/simple-rest
yarn add @refinedev/core @refinedev/react-router @refinedev/simple-rest
We start by creating
RefineContextcomponent inrefine/refine-context.tsxfile. This file will be used to wrap/refineroutes of our application.And then in
App.tsxfile, we are adding a newRoutecomponent withpath="/refine"and wrapping it withRefineContextcomponent.Finally, we create
refine/pages/products/list.tsxfile, here we can use Refine features, since it's layout is wrapped withRefinecomponent.
Sandpack component is temporarily disabled for compatibility reasons.
Next.js App
First, we need to install necessary packages:
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
yarn add @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
- We start by creating
app/refine/layout.tsxfile, this layout will be used by all pages under/refinefolder. - Then we create
app/refine/products/page.tsxfile, here we can use Refine features, since it's layout is wrapped withRefinecomponent.
Sandpack component is temporarily disabled for compatibility reasons.
Next.js Pages
First, we need to install necessary packages:
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
yarn add @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
- We start by creating
src/components/layout.tsxfile, this component will be conditionally rendered bypages/_app.tsxfile. - Then we create
pages/_app.tsxfile, here we are checking if the current file hasgetLayoutfunction, if it does, we are rendering it by wrapping it withgetLayoutfunction. - Then we create
pages/refine/products.tsxfile, here we are addingPage.getLayoutto our component, so it will be wrapped withRefinecontext. Then we can use Refine features, since it's layout is wrapped withRefinecomponent.
Sandpack component is temporarily disabled for compatibility reasons.
Adding UI to Router Examples
In the following examples below, as a follow-up from the previous router examples, we will add Ant Design layout from @refinedev/antd package.
See the UI Libraries guide for more information.
- Vite
- Next.js App
- Next.js Pages
Vite
First, we need to install necessary packages:
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/react-router @refinedev/antd @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/react-router @refinedev/antd @refinedev/simple-rest
yarn add @refinedev/core @refinedev/react-router @refinedev/antd @refinedev/simple-rest
We start by modifying
refine-context.tsxfile, adding necessary imports from@refinedev/antdpackage.And then in
App.tsxfile, we are updating ourErrorComponentimport from@refinedev/coreto@refinedev/antd.Finally, in
refine/pages/products/list.tsxfile, we are importingListcomponent anduseTablehook from@refinedev/antdpackage.
Sandpack component is temporarily disabled for compatibility reasons.
Next.js App
First, we need to install necessary packages:
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
yarn add @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
- We start by modifying
app/refine/layout.tsxfile, adding necessary imports from@refinedev/antdpackage. - Then we modify
app/refine/products/page.tsxfile, here we are usingListcomponent anduseTablehook from@refinedev/antdpackage.
Sandpack component is temporarily disabled for compatibility reasons.
Next.js Pages
First, we need to install necessary packages:
- npm
- pnpm
- yarn
npm i @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
pnpm add @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
yarn add @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
- We start by updating
src/components/layout.tsxfile, adding necessary imports from@refinedev/antdpackage. - Then we modify
pages/refine/products.tsxfile, here we are usingListcomponent anduseTablehook from@refinedev/antdpackage.
Sandpack component is temporarily disabled for compatibility reasons.
Authentication
If want to use Refine with your existing application, probably you already have authentication in-place. In this case, in order to enable Authentication features of Refine, only thing you need to do is to implement AuthProvider's check method.
If you want to handle Authentication with Refine from scratch, check the Authentication Guide
check Method
Once you provide the check method, you can use Authenticated component and/or useIsAuthenticated hook in your application. Refine will redirect user to given login page for unauthenticated users.
import { AuthProvider } from "@refinedev/core";
export const authProvider: AuthProvider = {
check: async () => {
const isAuthenticated = myCheckLogic();
if (isAuthenticated) {
return { authenticated: true };
}
return {
authenticated: false,
redirectTo: "/my-login-page",
error: {
name: "Authentication Failed.",
message: "User not found.",
},
};
},
login: async () => {
throw new Error("Method not implemented.");
},
logout: async () => {
throw new Error("Method not implemented.");
},
onError: async () => {
throw new Error("Method not implemented.");
},
};
Optional Methods
Following methods are optional, but could be useful for various use-cases.
getIdentity Method
getIdentity method can be used to enable useGetIdentity hook.
This hook is also used to rendering current user information in the header of UI Integration layouts.
onError
logout
logout method can be used to enable useLogout hook
This hook is also used to render Logout button in the sider of UI Integration layouts.