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.
Code Example
// file: /App.tsx
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
import { OtherComponent } from "./src/other-component";
import { RefineComponent } from "./src/refine-component";
const API_URL = "https://api.fake-rest.refine.dev";
export default function App() {
return (
<>
<Refine dataProvider={dataProvider(API_URL)}>
{/* You can use Refine hooks here */}
<OtherComponent />
<RefineComponent />
</Refine>
</>
);
}
// file: src/refine-component.tsx
import { useShow } from "@refinedev/core";
export const RefineComponent = () => {
const { query } = useShow({ resource: "products", id: 1 });
return (
<div>
<p>Hello From My Refine Component!</p>
<code>{`const { query } = useShow({ resource: "products", id: 1 });`}</code>
<p>useShow hook query:</p>
<code>{JSON.stringify(query.data, null, 2)}</code>
</div>
);
};// file: src/other-component.tsx
export const OtherComponent = () => {
return (
<div>
<p>Hello From My Existing Component</p>
<p>This component represents your existing components.</p>
<hr />
</div>
);
};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.
Code Example
// file: app/layout.tsx
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
const API_URL = "https://api.fake-rest.refine.dev";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<Refine dataProvider={dataProvider(API_URL)}>{children}</Refine>
</body>
</html>
);
}// file: app/refine/page.tsx
import Link from "next/link";
import { useShow } from "@refinedev/core";
export default function RefinePage() {
const { query } = useShow({ resource: "products", id: 1 });
return (
<div>
Hello From My Refine Component!
<hr />
<p>useShow hook query:</p>
<code>{JSON.stringify(query.data, null, 2)}</code>
<hr />
<Link href="/">Go to Home Page</Link>
</div>
);
}// file: app/page.tsx
import Link from "next/link";
export default function Home({ data }) {
return (
<div>
<h1>Home Page</h1>
<Link href="/other">Go to other page</Link>
<br />
<Link href="/refine">Go to Refine page</Link>
</div>
);
}// file: app/other/page.tsx
import Link from "next/link";
export default function OtherPage() {
return (
<div>
<h1>Hello From Other Page</h1>
<Link href="/">Go to Home Page</Link>
</div>
);
}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.
Code Example
// file: pages/_app.tsx
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
const API_URL = "https://api.fake-rest.refine.dev";
export default function MyApp({ Component, pageProps }) {
return (
<Refine dataProvider={dataProvider(API_URL)}>
<Component {...pageProps} />
</Refine>
);
}// file: pages/refine.tsx
import Link from "next/link";
import { useShow } from "@refinedev/core";
export default function RefinePage() {
const { query } = useShow({ resource: "products", id: 1 });
return (
<div>
Hello From My Refine Component!
<hr />
<p>useShow hook query:</p>
<code>{JSON.stringify(query.data, null, 2)}</code>
<hr />
<Link href="/">Go to Home Page</Link>
</div>
);
}// file: pages/index.tsx
import Link from "next/link";
export default function Home({ data }) {
return (
<div>
<h1>Hello From Home</h1>
<Link href="/other">Go to Other Page</Link>
<br />
<Link href="/refine">Go to Refine Page</Link>
</div>
);
}// file: pages/other.tsx
export default function OtherPage() {
return (
<div>
<h1>Hello From Other Page</h1>
<Link href="/">Go to Home Page</Link>
</div>
);
}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.
Code Example
// file: /refine/refine-context.tsx
import { Refine } from "@refinedev/core";
import routerProvider from "@refinedev/react-router";
import dataProvider from "@refinedev/simple-rest";
export function RefineContext({ children }) {
return (
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
resources={[
{
name: "products",
list: "/refine/products",
},
]}
options={{ syncWithLocation: true }}
>
{children}
</Refine>
);
}// file: /App.tsx
import { BrowserRouter, Route, Routes, Outlet } from "react-router";
import { ErrorComponent } from "@refinedev/core";
import { NavigateToResource } from "@refinedev/react-router";
import { Home } from "./pages/home.tsx";
import { About } from "./pages/about.tsx";
import { ProductList } from "./refine/pages/products/list.tsx";
import { RefineContext } from "./refine/refine-context.tsx";
export default function App() {
return (
<BrowserRouter>
<Routes>
{/* Your existing routes */}
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
{/* Refine routes */}
<Route
path="/refine"
element={
<RefineContext>
<Outlet />
</RefineContext>
}
>
<Route index element={<NavigateToResource />} />
<Route path="products" element={<ProductList />} />
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</BrowserRouter>
);
}// file: /refine/pages/products/list.tsx
import { useList } from "@refinedev/core";
export const ProductList = () => {
const { result } = useList();
const products = result?.data;
return (
<div>
<h1>Refine Products Page</h1>
<ul>
{products?.map((record) => (
<li key={record.id}>{record.name}</li>
))}
</ul>
</div>
);
};// file: /pages/home.tsx
export const Home = () => {
return (
<>
<h1>Home Page</h1>
<p>This file represents your existing page.</p>
<p>This component isn't wrapped with Refine context.</p>
<a href="/about">Go to About page</a>
<br />
<a href="/refine">Go to Refine page</a>
</>
);
};// file: /pages/about.tsx
export const About = () => {
return (
<div>
<h1>About Page</h1>
<a href="/">Go to Home page</a>
</div>
);
};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.
Code Example
// file: /app/refine/layout.tsx
"use client";
import { Refine } from "@refinedev/core";
import routerProvider from "@refinedev/nextjs-router";
import dataProvider from "@refinedev/simple-rest";
export default function RefineLayout({ children }: { children: React.ReactNode }) {
return (
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
resources={[
{
name: "products",
list: "/refine/products",
},
]}
options={{ syncWithLocation: true }}
>
{children}
</Refine>
);
}// file: /app/refine/products/page.tsx
"use client";
import { useList } from "@refinedev/core";
export default function Products() {
const { result } = useList();
const products = result?.data;
return (
<div>
<h1>Refine Products Page</h1>
<ul>
{products?.map((record) => (
<li key={record.id}>{record.name}</li>
))}
</ul>
</div>
);
}// file: /app/page.tsx
import Link from "next/link";
export default function Home() {
return (
<main>
<h1>Home Page</h1>
<Link href="/about">Go to About page</Link>
<br />
<Link href="/refine/products">Go to Refine page</Link>
</main>
);
}// file: /app/about/page.tsx
import Link from "next/link";
export default function AboutPage() {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home page</Link>
</div>
);
}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.
Code Example
// file: /src/components/refine-layout.tsx
import { Refine } from "@refinedev/core";
import routerProvider from "@refinedev/nextjs-router/pages";
import dataProvider from "@refinedev/simple-rest";
export default function RefineLayout({ children }) {
return (
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
resources={[
{
name: "products",
list: "/refine/products",
},
]}
options={{ syncWithLocation: true }}
>
{children}
</Refine>
</AntdApp>
</ConfigProvider>
);
}// file: /pages/_app.tsx
import type { ReactElement, ReactNode } from "react";
import type { NextPage } from "next";
import type { AppProps } from "next/app";
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (page: ReactElement) => ReactNode;
};
type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};
export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout ?? ((page) => page);
return getLayout(<Component {...pageProps} />);
}// file: /pages/refine/products.tsx
import type { ReactElement } from "react";
import { useList } from "@refinedev/core";
import RefineLayout from "../../src/components/refine-layout";
import type { NextPageWithLayout } from "../_app";
const Page: NextPageWithLayout = () => {
const { result } = useList();
const products = result?.data;
return (
<div>
<h1>Refine Products Page</h1>
<ul>
{products?.map((record) => (
<li key={record.id}>{record.name}</li>
))}
</ul>
</div>
);
};
Page.getLayout = function getLayout(page: ReactElement) {
return <RefineLayout>{page}</RefineLayout>;
};
export default Page;// file: /pages/about.tsx
import Link from "next/link";
export default function AboutPage() {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home page</Link>
</div>
);
}// file: /pages/index.tsx
import Link from "next/link";
export default function Home() {
return (
<>
<h1>Home Page</h1>
<Link href="/about">Go to About page</Link>
<br />
<Link href="/refine/products">Go to Refine page</Link>
</>
);
}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.
Code Example
// file: /refine/refine-context.tsx
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router";
import { RefineThemes, ThemedLayout } from "@refinedev/antd";
import { App as AntdApp, ConfigProvider } from "antd";
import "@refinedev/antd/dist/reset.css";
export function RefineContext({ children }) {
return (
<ConfigProvider theme={RefineThemes.Blue}>
<AntdApp>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
resources={[
{
name: "products",
list: "/refine/products",
},
]}
options={{ syncWithLocation: true }}
>
<ThemedLayout>{children}</ThemedLayout>
</Refine>
</AntdApp>
</ConfigProvider>
);
}// file: /App.tsx
import { BrowserRouter, Route, Routes, Outlet } from "react-router";
import { ErrorComponent } from "@refinedev/antd";
import { NavigateToResource } from "@refinedev/react-router";
import { Home } from "./pages/home.tsx";
import { About } from "./pages/about.tsx";
import { ProductList } from "./refine/pages/products/list.tsx";
import { RefineContext } from "./refine/refine-context.tsx";
export default function App() {
return (
<BrowserRouter>
<Routes>
{/* Your existing routes */}
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
{/* Refine routes */}
<Route
path="/refine"
element={
<RefineContext>
<Outlet />
</RefineContext>
}
>
<Route index element={<NavigateToResource />} />
<Route path="products" element={<ProductList />} />
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</BrowserRouter>
);
}// file: /refine/pages/products/list.tsx
import { List, useTable } from "@refinedev/antd";
import { Table } from "antd";
export const ProductList = () => {
const { tableProps } = useTable();
return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="Id" />
<Table.Column dataIndex="name" title="Name" />
<Table.Column dataIndex="price" title="Price" />
</Table>
</List>
);
};// file: /pages/home.tsx
export const Home = () => {
return (
<>
<h1>Home Page</h1>
<p>This file represents your existing page.</p>
<p>This component isn't wrapped with Refine context.</p>
<a href="/about">Go to About page</a>
<br />
<a href="/refine">Go to Refine page</a>
</>
);
};// file: /pages/about.tsx
export const About = () => {
return (
<div>
<h1>About Page</h1>
<a href="/">Go to Home page</a>
</div>
);
};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.
Code Example
// file: /app/refine/layout.tsx
"use client";
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/nextjs-router";
import { RefineThemes, ThemedLayout } from "@refinedev/antd";
import { App as AntdApp, ConfigProvider } from "antd";
import "@refinedev/antd/dist/reset.css";
export default function RefineLayout({ children }: { children: React.ReactNode }) {
return (
<ConfigProvider theme={RefineThemes.Blue}>
<AntdApp>
<Refine
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
routerProvider={routerProvider}
resources={[
{
name: "products",
list: "/refine/products",
},
]}
options={{ syncWithLocation: true }}
>
<ThemedLayout>{children}</ThemedLayout>
</Refine>
</AntdApp>
</ConfigProvider>
);
}// file: /app/refine/products/page.tsx
"use client";
import { List, useTable } from "@refinedev/antd";
import { Table } from "antd";
export default function Products() {
const { tableProps } = useTable();
return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="Id" />
<Table.Column dataIndex="name" title="Name" />
<Table.Column dataIndex="price" title="Price" />
</Table>
</List>
);
}// file: /app/page.tsx
import Link from "next/link";
export default function Home() {
return (
<main>
<h1>Home Page</h1>
<Link href="/about">Go to About page</Link>
<br />
<Link href="/refine/products">Go to Refine page</Link>
</main>
);
}// file: /app/about/page.tsx
import Link from "next/link";
export default function AboutPage() {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home page</Link>
</div>
);
}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.
Code Example
// file: /src/components/refine-layout.tsx
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/nextjs-router/pages";
import { RefineThemes, ThemedLayout } from "@refinedev/antd";
import { App as AntdApp, ConfigProvider } from "antd";
import "@refinedev/antd/dist/reset.css";
export default function RefineLayout({ children }) {
return (
<ConfigProvider theme={RefineThemes.Blue}>
<AntdApp>
<Refine
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
routerProvider={routerProvider}
resources={[
{
name: "products",
list: "/refine/products",
},
]}
options={{ syncWithLocation: true }}
>
<ThemedLayout>{children}</ThemedLayout>
</Refine>
</AntdApp>
</ConfigProvider>
);
}// file: /pages/_app.tsx
import type { ReactElement, ReactNode } from "react";
import type { NextPage } from "next";
import type { AppProps } from "next/app";
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (page: ReactElement) => ReactNode;
};
type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};
export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout ?? ((page) => page);
return getLayout(<Component {...pageProps} />);
}// file: /pages/refine/products.tsx
import type { ReactElement } from "react";
import { List, useTable } from "@refinedev/antd";
import { Table } from "antd";
import RefineLayout from "../../src/components/refine-layout";
import type { NextPageWithLayout } from "../_app";
const Page: NextPageWithLayout = () => {
const { tableProps } = useTable();
return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="Id" />
<Table.Column dataIndex="name" title="Name" />
<Table.Column dataIndex="price" title="Price" />
</Table>
</List>
);
};
Page.getLayout = function getLayout(page: ReactElement) {
return <RefineLayout>{page}</RefineLayout>;
};
export default Page;// file: /pages/about.tsx
import Link from "next/link";
export default function AboutPage() {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home page</Link>
</div>
);
}// file: /pages/index.tsx
import Link from "next/link";
export default function Home() {
return (
<>
<h1>Home Page</h1>
<Link href="/about">Go to About page</Link>
<br />
<Link href="/refine/products">Go to Refine page</Link>
</>
);
}// file: /next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
transpilePackages: [
"@ant-design/icons-svg",
"@refinedev/nextjs-router",
"@refinedev/antd",
"antd",
"@ant-design/pro-components",
"@ant-design/pro-layout",
"@ant-design/pro-utils",
"@ant-design/pro-provider",
"rc-pagination",
"rc-picker",
"rc-util",
],
};
module.exports = nextConfig;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.
