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
pnpmadd @refinedev/core @refinedev/simple-rest
yarnadd @refinedev/core @refinedev/simple-rest
App.tsx
import{Refine}from"@refinedev/core"; importdataProviderfrom"@refinedev/simple-rest"; constAPI_URL="https://api.fake-rest.refine.dev"; functionApp(){ return( <ExistingProvider> <RefinedataProvider={dataProvider(API_URL)}> {/* You can use Refine hooks inside here */} <ComponentWithRefineHooks/> <ExistingComponent1> </Refine> </ExistingProvider> ); }
The following example shows how to use Refine's useShow hook with an existing application.
Vite
Next.js App
Next.js Pages
As you can see in the example below, wrapping App.tsx file with Refine component is enough to use Refine hooks inside your application.
import{Refine}from"@refinedev/core";importdataProviderfrom"@refinedev/simple-rest";import{OtherComponent}from"./src/other-component";import{RefineComponent}from"./src/refine-component";constAPI_URL = "https://api.fake-rest.refine.dev";exportdefaultfunctionApp(){return(<><RefinedataProvider={dataProvider(API_URL)}>{/* You can use Refine hooks here */}
<OtherComponent/><RefineComponent/></Refine></>);}
Content: export const OtherComponent = () => {
return (
<div>
<p>Hello From My Existing Component</p>
<p>This component represents your existing components.</p>
<hr />
</div>
);
};
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.
Content: 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
Content: import Link from "next/link";
import { useShow } from "@refinedev/core";
export default function RefinePage() {
const { queryResult } = useShow({ resource: "products", id: 1 });
return (
<div>
Hello From My Refine Component!
<hr />
<p>useShow hook queryResult:</p>
<code>{JSON.stringify(queryResult.data, null, 2)}</code>
<hr />
<Link href="/">Go to Home Page</Link>
</div>
);
}
File: app/page.tsx
Content: 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
Content: 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>
);
}
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.
Content: import Link from "next/link";
import { useShow } from "@refinedev/core";
export default function RefinePage() {
const { queryResult } = useShow({ resource: "products", id: 1 });
return (
<div>
Hello From My Refine Component!
<hr />
<p>useShow hook queryResult:</p>
<code>{JSON.stringify(queryResult.data, null, 2)}</code>
<hr />
<Link href="/">Go to Home Page</Link>
</div>
);
}
File: pages/index.tsx
Content: 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
Content: export default function OtherPage() {
return (
<div>
<h1>Hello From Other Page</h1>
<Link href="/">Go to Home Page</Link>
</div>
);
}
Content: export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
File: /app/page.tsx
Content: 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
Content: import Link from "next/link";
export default function AboutPage() {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home page</Link>
</div>
);
}
First, we need to install necessary packages:
npm
pnpm
yarn
npm i @refinedev/core @refinedev/nextjs-router @refinedev/simple-rest
We start by creating src/components/layout.tsx file, this component will be conditionally rendered by pages/_app.tsx file.
Then we create pages/_app.tsx file, here we are checking if the current file has getLayout function, if it does, we are rendering it by wrapping it with getLayout function.
Then we create pages/refine/products.tsx file, here we are adding Page.getLayout to our component, so it will be wrapped with Refine context. Then we can use Refine features, since it's layout is wrapped with Refine component.
Content: 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
Content: 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
Content: 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 { data: products } = useList();
return (
<div>
<h1>Refine Products Page</h1>
<ul>
{products?.data?.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
Content: 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
Content: 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>
</>
);
}
Content: export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
File: /app/page.tsx
Content: 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
Content: import Link from "next/link";
export default function AboutPage() {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home page</Link>
</div>
);
}
First, we need to install necessary packages:
npm
pnpm
yarn
npm i @refinedev/core @refinedev/nextjs-router @refinedev/antd @refinedev/simple-rest
Content: import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/nextjs-router/pages";
import { RefineThemes, ThemedLayoutV2 } 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 }}
>
<ThemedLayoutV2>{children}</ThemedLayoutV2>
</Refine>
</AntdApp>
</ConfigProvider>
);
}
File: /pages/_app.tsx
Content: 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
Content: 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
Content: 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
Content: 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>
</>
);
}
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
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"; exportconst 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()=>{ thrownewError("Method not implemented."); }, logout:async()=>{ thrownewError("Method not implemented."); }, onError:async()=>{ thrownewError("Method not implemented."); }, };