React-admin: ReferenceInput loads its data before its containing view

Created on 3 Dec 2020  路  11Comments  路  Source: marmelab/react-admin

RefereneInput loads its items before its own container.
This is a problem for me, because my api sets up at the server what the RefereneInput is going to load.
I don't know if this is a bug or a limitation or how to change the order of the calls.

screen-shot

https://codesandbox.io/s/heuristic-kepler-m2zkf?file=/src/index.js (Just in case)

All 11 comments

For me, I got the opposite one (your expectation) after opening page directly /posts/2.
image

I still get the opposite order of what you get with /post/2
Thanks for your input

Could it be related to the time zone we are using some how?
Since is the same sandbox

Nope, I don't think so.

I don't get it.
I've tried it in many computers with the latest browsers and all do the same, first the ReferenceInput call then the Edit api call

I have the effect you get when first loading the resource, but every refresh does it the other way around.
image

You could change the order of the call with something like this:

import { useForm } from "react-final-form";

const DelayedReference = ({ record = {}, source, classes, ...props }) => {
  const form = useForm();
  let is_id_set = form.getFieldState("id");

  if (is_id_set) {
    return (
      <ReferenceInput label="User" source="user_id" reference="users">
        <AutocompleteInput />
      </ReferenceInput>
    );
  } else {
    return "Nothing yet.";
  }
};

const PostEdit = ({ permissions, ...props }) => (
  <Edit {...props}>
    <SimpleForm>
      <TextInput disabled source="id" />
      <DelayedReference />
    </SimpleForm>
  </Edit>
);

It waits for the id to be set to actually create your reference input forcing it to be later then the Edit.
Don't know if this is intented though from RA.

@MicroJackson,
Thank you very much for the workaround.
Very simple idea, seems to work.
Even do I would love to hear from RA team regarding this behaviour.

We haven't thought about doing these queries in a particular order. It's a result of the react rendering and effect process.

I don't think it's a bug. And if it matters, as in your case, you can use @MicroJackson's trick.

So 4Im closing this issue.

@MicroJackson,
I end up using useVersion hook for the task, just sharing:

```jsx
import * as React from "react";
import {useEffect, useRef} from "react";
import {ReferenceInput, AutocompleteInput, useVersion} from "react-admin";

const DelayedReferenceInput = props => {
const version = useVersion();
const parentLoaded = useRef(false);

useEffect(() => {
    parentLoaded.current = true;
}, [version]);

if (parentLoaded.current) {
  return (
    <ReferenceInput {...props}>
      <AutocompleteInput />
    </ReferenceInput>
  );
} else {
  return "Nothing yet.";
}

};

@WiXSL
Thanks for sharing!
Just curious, is there any benefit of writing it this way instead of the useForm?

@MicroJackson,
Your example work for me only the first time, using useVersion allows me to re-fetch after each refresh of the form's data.
React-admin's fetch data hooks use it.

Cool, thanks for the explanation.
Learned something again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkyrychuk picture rkyrychuk  路  3Comments

pixelscripter picture pixelscripter  路  3Comments

phacks picture phacks  路  3Comments

ericwb picture ericwb  路  3Comments

yangjiamu picture yangjiamu  路  3Comments