documentation/versioned_docs/version-3.xx.xx/tutorial/1-getting-started/mui/3-generate-crud-pages.md
Inferencer is a powerful tool in the refine ecosystem that helps developers quickly generate CRUD (create, read, update, delete) pages for their data model. It analyzes your data model based on the resource scheme and automatically creates the pages with required forms and tables for CRUD operations.
There are several benefits:
The goal of Inferencer is to reduce the amount of time spent on creating views for resources by generating the code automatically. This can save developers a significant amount of time and effort, especially on large projects with many resources.
Inferencer generates code that is easy to customize. Instead of starting from scratch, you can use the auto-generated code as a starting point and make changes to fit your specific needs. This allows you to quickly iterate and get your project up and running faster.
By using Inferencer, you can avoid common mistakes that can arise when building CRUD pages manually. This can help you avoid bugs and other issues that can arise during development.
Overall, using Inferencer can greatly speed up development time and reduce the amount of code that needs to be written manually. It helps ensure that your CRUD pages are consistent and adhere to best practices, freeing you up to focus on more complex tasks.
The @pankod/refine-inferencer package provides the <MuiInferencer/> component, which can be imported from @pankod/refine-inferencer/mui. It used to generate CRUD pages based on your API response with Material UI components.
The <MuiInferencer/> component is used by passing to appropriate values in the resources prop of the <Refine/> component in App.tsx as shown below:
:::info
In Unit 4, the resources concept will be explained in detail. For now, you can assume that the resource is a collection of data on your API used in the app.
:::
import { Refine } from "@pankod/refine-core";
import {
Layout,
ReadyPage,
ErrorComponent,
LightTheme,
CssBaseline,
GlobalStyles,
ThemeProvider,
RefineSnackbarProvider,
useNotificationProvider,
} from "@pankod/refine-mui";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
//highlight-next-line
import { MuiInferencer } from "@pankod/refine-inferencer/mui";
const App: React.FC = () => {
return (
<ThemeProvider theme={LightTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
notificationProvider={useNotificationProvider}
Layout={Layout}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
//highlight-start
resources={[
{
name: "blog_posts",
list: MuiInferencer,
show: MuiInferencer,
create: MuiInferencer,
edit: MuiInferencer,
},
]}
//highlight-end
/>
</RefineSnackbarProvider>
</ThemeProvider>
);
};
export default App;
At this point, refine will automatically generate the CRUD pages for the blog_posts resource based on the API response.
So far we have added complete CRUD pages by utilizing Inferencer feature to the project. Let's take a look at the auto-generated CRUD pages that Inferencer handles for us.
Before we start, let's understand the API that we will be using in this tutorial. We will be using the https://api.fake-rest.refine.dev API. This API is created by the refine team and is used for demo purposes. It is a simple REST API that contains some resources like blog_posts, categories, etc.
:::info
In Unit 3, it will be explained in detail how refine apps communicate with the API via the dataProvider.
:::
You can view the code generated by Inferencer by clicking the "Show Code" button in the right corner of the live preview below.
To preview the list page, go back to your browser and open the <a href="localhost:3000" rel="noopener noreferrer nofollow">localhost:3000</a> address.
You should see the list page for the blog_posts resource that was generated by Inferencer like below:
:::note
When you navigate to the localhost:3000, refine will redirect you to the initial resource's list page registered to <Refine/> component like localhost:3000/blog-posts
:::
setInitialRoutes(["/blog-posts"]);
import { Refine } from "@pankod/refine-core";
import {
Layout,
ReadyPage,
ErrorComponent,
LightTheme,
CssBaseline,
GlobalStyles,
ThemeProvider,
RefineSnackbarProvider,
useNotificationProvider,
} from "@pankod/refine-mui";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import { MuiInferencer } from "@pankod/refine-inferencer/mui";
const App: React.FC = () => {
return (
<ThemeProvider theme={LightTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
notificationProvider={useNotificationProvider}
Layout={Layout}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
resources={[
{
name: "blog_posts",
list: MuiInferencer,
show: MuiInferencer,
create: MuiInferencer,
edit: MuiInferencer,
},
]}
/>
</RefineSnackbarProvider>
</ThemeProvider>
);
};
render(<App />);
To preview the create page, go back to your browser and open the <a href="localhost:3000/blog-posts/create" rel="noopener noreferrer nofollow">localhost:3000/blog-posts/create</a> address or click the "Create" button on the list page.
You should see the create page for the blog_posts resource that was generated by Inferencer like below:
setInitialRoutes(["/blog-posts/create"]);
import { Refine } from "@pankod/refine-core";
import {
Layout,
ReadyPage,
ErrorComponent,
LightTheme,
CssBaseline,
GlobalStyles,
ThemeProvider,
RefineSnackbarProvider,
useNotificationProvider,
} from "@pankod/refine-mui";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import { MuiInferencer } from "@pankod/refine-inferencer/mui";
const App: React.FC = () => {
return (
<ThemeProvider theme={LightTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
notificationProvider={useNotificationProvider}
Layout={Layout}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
resources={[
{
name: "blog_posts",
list: MuiInferencer,
show: MuiInferencer,
create: MuiInferencer,
edit: MuiInferencer,
},
]}
/>
</RefineSnackbarProvider>
</ThemeProvider>
);
};
render(<App />);
To preview the edit page, go back to your browser and open the <a href="localhost:3000/blog-posts/edit/123" rel="noopener noreferrer nofollow">localhost:3000/blog-posts/edit/123</a> address or click the "Edit" button on the record in the list page.
You should see the edit page for the blog_posts resource with the id 123 that was generated by Inferencer like below:
setInitialRoutes(["/blog-posts/edit/123"]);
import { Refine } from "@pankod/refine-core";
import {
Layout,
ReadyPage,
ErrorComponent,
LightTheme,
CssBaseline,
GlobalStyles,
ThemeProvider,
RefineSnackbarProvider,
useNotificationProvider,
} from "@pankod/refine-mui";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import { MuiInferencer } from "@pankod/refine-inferencer/mui";
const App: React.FC = () => {
return (
<ThemeProvider theme={LightTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
notificationProvider={useNotificationProvider}
Layout={Layout}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
resources={[
{
name: "blog_posts",
list: MuiInferencer,
show: MuiInferencer,
create: MuiInferencer,
edit: MuiInferencer,
},
]}
/>
</RefineSnackbarProvider>
</ThemeProvider>
);
};
render(<App />);
To preview the show page, go back to your browser and open the <a href="localhost:3000/blog-posts/show/123" rel="noopener noreferrer nofollow">localhost:3000/blog-posts/show/123</a> address or click the "Show" button on the record in the list page.
You should see the show page for the blog_posts resource with the id 123 that was generated by Inferencer like below:
setInitialRoutes(["/blog-posts/show/123"]);
import { Refine } from "@pankod/refine-core";
import {
Layout,
ReadyPage,
ErrorComponent,
LightTheme,
CssBaseline,
GlobalStyles,
ThemeProvider,
RefineSnackbarProvider,
useNotificationProvider,
} from "@pankod/refine-mui";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import { MuiInferencer } from "@pankod/refine-inferencer/mui";
const App: React.FC = () => {
return (
<ThemeProvider theme={LightTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider("https://api.fake-rest.refine.dev")}
notificationProvider={useNotificationProvider}
Layout={Layout}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
resources={[
{
name: "blog_posts",
list: MuiInferencer,
show: MuiInferencer,
create: MuiInferencer,
edit: MuiInferencer,
},
]}
/>
</RefineSnackbarProvider>
</ThemeProvider>
);
};
render(<App />);
In Unit 5, you will learn how to create CRUD pages manually using the code generated by Inferencer as a reference.
<Checklist> <ChecklistItem id="generated-crud-pages"> I understood what is Inferencer, how it works and how Inferencer make my work easier. </ChecklistItem> <ChecklistItem id="generated-crud-pages-2">I inspected the auto-generated list, create, edit, and show pages.