Home > Software engineering >  async return the simpleRestProvider in react admin
async return the simpleRestProvider in react admin

Time:11-11

simple way to import dataProvider

const dataProvider = simpleRestProvider(apiHost, httpClient);
export default dataProvider;

How can to export async?

export default async function () {
   return simpleRestProvider(apiHost, httpClient);
};

by using in this I get a error in admin page where I need getList from the dataProvider

The response to 'getList' must be like { data: ... }, but the received response does not have a 'data' key. The dataProvider is probably wrong for 'getList'.

CodePudding user response:

Initialize your dataProvider in an effect:

const MyAdmin = () => {
    const [dataProvider, setDataProvider] = useState();

    useEffect(() => {
        getDataProvider().then(dataProvider => setDataProvider(dataProvider));
    }, []);

    if (!dataProvider) {
        return null; // or a loading page, whatever
    }

    return (
        <Admin dataProvider={dataProvider}>
            // ...
        </Admin>
    );
}

CodePudding user response:

use this syntax, function name is missing, might be that is issue, i hope after adding correct syntax of export function code will work for sure,

export default async function dataProvider() {
   return await simpleRestProvider(apiHost, httpClient);
};

import { dataProvider } from 'your_path_here';

or you can expor with this syntax,

export const dataProvider = async () => {        
    return await simpleRestProvider(apiHost, httpClient);
};
  • Related