-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Fix warning when passing partial props to useListContext and other view context hooks #5802
Conversation
…ng props overrides
fcac390
to
557c26c
Compare
@Luwangel FYI, this PR broke our production system today. Seems We were on |
@stensrud Would you mind opening a new issue for your problem, following the issue template (and providing a way to reproduce the problem)? |
React-admin wraps lists, create, edit and show views inside contexts. The main goal is to avoid passing props from the top component to every child one by one.
For example an Edit View is composed of multiple layout elements:
...
And these layout elements are composed of atomic elements (and some of them are iterator displaying smaller elements):
Thanks to the modularity of react-admin, you can write your own atomic elements.
Nevertheless, from a developer perspective, it's difficult to know which props are implicitly injected from the top because there are too many levels of components. And the reason why we added the contexts.
For example, imagine you want to create a custom iterator (a customized list of elements). Thanks to the contexts, you just need to connect the
useListContext
hook and directly get the props you need like the loaded state, the data, the total number of elements.When we developed the contexts, we deprecated the props mechanism with the intention to remove it in the next major version (v4). During this time, we encouraged the users to change their custom code by displaying a warning:
But, as a result, some smaller components became unusable without surrounding them by the appropriate context. It's the case of the
<Datagrid>
which now calls theuseListContext
.And it's not a pleasant developer experience because it means adding a
<ListContext>
around your custom iterator. That's why we decided to rely on both props and context.Starting from this PR, you can override the value of the
useListContext
, theuseEditContext
, theuseCreateContext and the
useShowContext` hooks.It's as simple as passing arguments to a function, and the props will take precedence over the values of the context.
const context = useListContext(props);
If there is no context, it works too like this
<Datagrid>
(see the screenshot attached):Todo
useEditContext
, theuseCreateContext
and theuseShowContext
hooks to use propsScreenshot
It's now possible to use a
Datagrid
without putting it inside aListContext
: