react-admin List组件背景Card移除/隐藏方案咨询(v3.4.2)
Got it, let's tackle how to remove that default background Card from react-admin v3.4.2's List component without those pesky console errors you mentioned. The component prop hack you tried isn't documented and causes issues, so here are two reliable approaches:
1. Customize a Card-Free List Component (Recommended)
Looking at the react-admin List source code, the component wraps its content in a Material-UI Card by default. To avoid this, we can build our own List component that reuses react-admin's core logic (like data fetching, context, and toolbar) but skips the Card wrapper.
Here's a working implementation:
import React from 'react'; import { ListController, ListToolbar, Title } from 'react-admin'; import { makeStyles } from '@material-ui/core/styles'; const useStyles = makeStyles({ listRoot: { padding: '16px 0', // Adjust padding to match your layout needs width: '100%', }, }); export const NoCardList = ({ title, filters, actions, children, ...props }) => { const classes = useStyles(); return ( <ListController {...props}> {(controllerProps) => ( <div className={classes.listRoot}> {/* Optional: Keep the page title */} {title && <Title title={title} />} {/* Reuse the default List Toolbar for filters/actions */} <ListToolbar filters={filters} actions={actions} {...controllerProps} /> {/* Pass the list context to your child components (like Datagrid) */} {React.cloneElement(children, controllerProps)} </div> )} </ListController> ); };
You can use this component exactly like the default List:
<NoCardList resource="posts"> <Datagrid> <TextField source="id" /> <TextField source="title" /> </Datagrid> </NoCardList>
This approach is stable because it relies on react-admin's public API (ListController and ListToolbar) instead of undocumented props.
2. Override Styles (Quick Fix)
If you want a faster solution without writing a custom component, you can target the internal Card's CSS class to hide its background and styling. In react-admin v3.4.2, the List's Card uses the class ra-list-card.
Use Material-UI's makeStyles to override it:
import { makeStyles } from '@material-ui/core/styles'; import { List } from 'react-admin'; const useStyles = makeStyles({ noCardList: { '& .ra-list-card': { backgroundColor: 'transparent', boxShadow: 'none', border: 'none', padding: 0, }, }, }); const MyPostList = () => { const classes = useStyles(); return ( <List className={classes.noCardList} resource="posts"> <Datagrid> {/* Your columns */} </Datagrid> </List> ); };
This will make the Card visually disappear, though it's still present in the DOM. It's a quick fix but relies on internal class names, which could change if you upgrade react-admin (but since you're on v3.4.2, this is safe).
Are There Out-of-the-Box Components?
Unfortunately, react-admin v3 doesn't include an official Card-free List component. Your best bet is to use the custom component approach for long-term maintainability, or the style override for a quick win.
内容的提问来源于stack exchange,提问作者Artur Carvalho

