-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataGrid] Start the FAQ page #11686
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
--- | ||
productId: x-data-grid | ||
title: Frequently Asked Questions - React Data Grid | ||
githubLabel: 'component: data grid' | ||
packageName: '@mui/x-data-grid' | ||
--- | ||
|
||
# Frequently asked questions | ||
|
||
<p class="description">Can't find what you are looking for? The FAQ section has answers to some of the most frequent questions and challenges.</p> | ||
|
||
If you still have trouble, you can refer to the [support page](/x/introduction/support/). | ||
|
||
## How to pass props to the data grid for better performance? | ||
|
||
The Data Grid is a complex component that renders a lot of elements. As a general rule, all the non-primitive props should keep a stable reference between renders to avoid unnecessary re-renders. | ||
|
||
This is especially important for the `columns` prop. The columns are designed to be definitions, to never change once the component is mounted. Otherwise, you take the risk of losing elements like column width or order. | ||
|
||
This can be achieved by one of the following ways: | ||
|
||
1. If the columns do not depend on something within the component scope, you can define them outside of the component and pass them as a prop. | ||
|
||
```tsx | ||
const columns = [ | ||
{ field: 'id' }, | ||
{ field: 'firstName' }, | ||
{ field: 'lastName' }, | ||
{ field: 'age' }, | ||
{ field: 'fullName' }, | ||
]; | ||
|
||
function App() { | ||
return <DataGrid columns={columns} {...other} />; | ||
} | ||
``` | ||
|
||
2. If the columns require something within the component scope, like a state value or a prop, you can use the `useMemo` hook to keep a stable reference between renders. | ||
|
||
```tsx | ||
function App(props) { | ||
const renderCell = React.useCallback( | ||
(params) => { | ||
return ( | ||
<strong> | ||
{params.value} {props.someValue} | ||
</strong> | ||
); | ||
}, | ||
[props.someValue], | ||
); | ||
|
||
const columns = useMemo( | ||
MBilalShafi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
() => [ | ||
{ field: 'id' }, | ||
{ field: 'firstName' }, | ||
{ field: 'lastName' }, | ||
{ field: 'age' }, | ||
{ field: 'fullName', renderCell }, | ||
], | ||
[renderCell], | ||
); | ||
|
||
return <DataGrid columns={columns} {...other} />; | ||
} | ||
``` | ||
|
||
## What is the difference between renderCell, valueGetter, and valueFormatter? Which one should I use? | ||
Check warning on line 68 in docs/data/data-grid/faq/faq.md
|
||
|
||
The Data Grid provides several ways to customize the content of a cell. Each of them has a different purpose and should be used in different situations. Here is a summary of their differences: | ||
|
||
### `renderCell` | ||
|
||
It is the most powerful way to customize the content of a cell. It allows you to render anything you want inside the cell. It is the only way to render a React component inside a cell. It is also the only way to customize the cell's behavior (for instance, add a click handler). It is the most flexible way to customize a cell, but it is also the most expensive one. It should be used only when the other options are not enough. | ||
|
||
Here's an example for a cell that displays a button: | ||
|
||
```tsx | ||
const columns: GridColDef[] = [ | ||
{ | ||
field: 'date', | ||
headerName: 'Year', | ||
renderCell: (params: GridRenderCellParams<any, Date>) => ( | ||
<strong> | ||
{params.value.getFullYear()} | ||
<Button | ||
variant="contained" | ||
size="small" | ||
style={{ marginLeft: 16 }} | ||
tabIndex={params.hasFocus ? 0 : -1} | ||
> | ||
Open | ||
</Button> | ||
</strong> | ||
), | ||
}, | ||
]; | ||
``` | ||
|
||
See more about the `renderCell` method in the [rendering cells](/x/react-data-grid/column-definition/#rendering-cells) section. | ||
|
||
### `valueGetter` | ||
|
||
It is a function that allows you to derive the cell value from the row data. It is the most performant way to customize the cell content. It is also the only way to customize the cell value without changing the row data. It should be used when you need to derive the cell value from the row data. Common use cases are: | ||
|
||
- Transforming the value (e.g convert a decimal value to percentage value) | ||
Check warning on line 106 in docs/data/data-grid/faq/faq.md
|
||
MBilalShafi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Deriving the value from multiple fields (e.g. concatenating first name and last name) | ||
- Deriving the value from a nested field (e.g. `user.address.city`) | ||
|
||
This value is also used internally in the Grid to do filtering, sorting and rendering (if no `renderCell` or `valueFormatter` is provided). You can know more about it in the [value getter](/x/react-data-grid/column-definition/#value-getter) section. | ||
Check warning on line 110 in docs/data/data-grid/faq/faq.md
|
||
MBilalShafi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### `valueFormatter` | ||
|
||
It is a function that allows you to format the cell value. It could be used to customize the cell value without changing the row data. It should be used when you need to format the cell value. | ||
|
||
A few common use-cases are: | ||
|
||
- Formatting a date to a custom display format | ||
- Formatting a decimal value to percentage and show `%` sign | ||
- Formatting a boolean value to `Yes` or `No` | ||
|
||
It only impacts the rendering part and does not impact the internal calculations like filtering or sorting. You can know more about it in the [value formatter](/x/react-data-grid/column-definition/#value-formatter) section. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,6 +235,7 @@ const pages: MuiPage[] = [ | |
}, | ||
], | ||
}, | ||
{ pathname: '/x/react-data-grid/faq', title: 'FAQ' }, | ||
], | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import * as pageProps from 'docsx/data/data-grid/faq/faq.md?@mui/markdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs {...pageProps} />; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a nice place to present answers to more general purpose questions we are asked often.
Would appreciate sharing of more points here.
CC @mui/xgrid