Closed thdk closed 1 year ago
This snippet strikes me:
const { data } = useGetList<Merchant>('merchants');
return (
<Create {...props}>
<SimpleForm>
{<SelectInput source="category" choices={data} />}
</SimpleForm>
</Create>
);
You shouldn't need to do that, and instead you should use <ReferenceInput>
.
That's also the reason why choices are not required: when inside a ReferenceInput
, the choices can be undefined, and then the SelectInput grabs them from the ChoicesContext filled by ReferenceInput.
So we won't remove that error - but we probably need to make it more explicit.
Oops! Thanks for point that out, just picked up on react-admin since a long time.
So I quickly went to create a PR to make this more explicit, however, it turns out it used to be explicit in the past. PR #7185 removed the warning when choices is undefined. Other input controls like CheckboxGroupInput
still have the warning in place. PR incoming :)
When you pass
undefined
as value to theSelectInput
component you'll get an error likecannot read property map of undefined
. So either the SelectInput component should be fixed to deal withchoices
beingundefined
or thechoices
prop should not be optional.What you were expecting: I did not expect to be able to build react-admin when
undefined
is used as value for thechoices
prop of theSelectInput
component. However, since I can build I would have expected an empty dropdown control to be shown.What happened instead: See stacktrace posted below.
Steps to reproduce:
Related code: Below code should not compile because
data
may be undefined on first render resulting in js errors when trying to access it's properties.Other information:
This can be fixed by removing the questionmark here: https://github.com/marmelab/react-admin/blob/6503cb3c6aa50f5048edf7bdcb5ee22fc3ce6c10/packages/ra-core/src/form/useChoices.tsx#L16
Environment
Stack trace (in case of a JS error): SelectInput.tsx:328 Uncaught TypeError: Cannot read properties of undefined (reading 'map') at SelectInput (SelectInput.tsx:328:31) at renderWithHooks (react-dom.development.js:16305:18) at mountIndeterminateComponent (react-dom.development.js:20074:13) at beginWork (react-dom.development.js:21587:16) at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14) at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16) at invokeGuardedCallback (react-dom.development.js:4277:31) at beginWork$1 (react-dom.development.js:27451:7) at performUnitOfWork (react-dom.development.js:26557:12) at workLoopSync (react-dom.development.js:26466:5) SelectInput @ SelectInput.tsx:328 renderWithHooks @ react-dom.development.js:16305 mountIndeterminateComponent @ react-dom.development.js:20074 beginWork @ react-dom.development.js:21587 callCallback2 @ react-dom.development.js:4164 invokeGuardedCallbackDev @ react-dom.development.js:4213 invokeGuardedCallback @ react-dom.development.js:4277 beginWork$1 @ react-dom.development.js:27451 performUnitOfWork @ react-dom.development.js:26557 workLoopSync @ react-dom.development.js:26466 renderRootSync @ react-dom.development.js:26434 recoverFromConcurrentError @ react-dom.development.js:25850 performSyncWorkOnRoot @ react-dom.development.js:26096 flushSyncCallbacks @ react-dom.development.js:12042 (anonymous) @ react-dom.development.js:25651 react_devtools_backend.js:4026 The above error occurred in the component:
at SelectInput (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:15595:27) at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at Grid2 (http://localhost:5173/node_modules/.vite/deps/chunk-635SWO65.js?v=9690cb63:12272:22) at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at Grid2 (http://localhost:5173/node_modules/.vite/deps/chunk-635SWO65.js?v=9690cb63:12272:22) at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at Stack2 (http://localhost:5173/node_modules/.vite/deps/chunk-635SWO65.js?v=9690cb63:22003:22) at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at CardContent2 (http://localhost:5173/node_modules/.vite/deps/chunk-635SWO65.js?v=9690cb63:8396:17) at DefaultComponent2 (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:17060:22) at form at FormGroupsProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:20361:21) at FormProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:15044:11) at LabelPrefixContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:19849:19) at OptionalRecordContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:13622:18) at Form2 (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:20756:24) at SimpleForm (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:17035:24) at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at Paper2 (http://localhost:5173/node_modules/.vite/deps/chunk-635SWO65.js?v=9690cb63:777:17) at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at Card2 (http://localhost:5173/node_modules/.vite/deps/chunk-635SWO65.js?v=9690cb63:8141:17) at div at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at CreateView (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:16172:23) at RecordContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:13252:21) at SaveContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:12905:21) at CreateContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:13286:21) at CreateBase (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:13312:21) at Create (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:16259:3) at ReservationCreate (http://localhost:5173/src/resources/reservations/reservation-create.tsx:23:7) at RenderedRoute (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9445:5) at Routes (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9645:5) at ResourceContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:19349:21) at Resource (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:19355:22) at RenderedRoute (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9445:5) at Routes (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9645:5) at ErrorBoundary2 (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:7734:35) at div at main at div at div at http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:5780:51 at Layout (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:8038:19) at div at RenderedRoute (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9445:5) at Routes (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9645:5) at CoreAdminRoutes (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:19234:28) at RenderedRoute (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9445:5) at Routes (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9645:5) at CoreAdminUI (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:19298:18) at AdminUI (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:20315:26) at InnerThemeProvider (http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:8148:17) at ThemeProvider2 (http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:7897:5) at ThemeProvider3 (http://localhost:5173/node_modules/.vite/deps/chunk-F7COIV5B.js?v=9690cb63:8156:5) at ThemeProvider (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:8524:22) at ResourceDefinitionContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:18862:15) at NotificationContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:5771:21) at I18nContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:19882:15) at Router (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:9592:15) at HistoryRouter2 (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:10441:21) at InternalRouter (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:10481:21) at BasenameContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:10461:21) at AdminRouter (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:10467:20) at QueryClientProvider2 (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:8522:21) at PreferencesEditorContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:18778:21) at StoreContextProvider (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:5679:18) at CoreAdminContext (http://localhost:5173/node_modules/.vite/deps/chunk-O7L5CXQL.js?v=9690cb63:18887:28) at AdminContext (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:20358:19) at Admin (http://localhost:5173/node_modules/.vite/deps/react-admin.js?v=9690cb63:20614:28) at Admin at App
React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary2.