如何在react-admin中拥有多个dataProviders的正确方法?

我正在尝试在react-admin项目中使用多个dataproviders,但出现错误:

Warning: Missing translation for key: "dataProvider is not a function"

function.console.(anonymous function) @ index.js:1452

我有App.js这样的:

import React from 'react';

import { render } from 'react-dom';

import { Admin, Resource } from 'react-admin';

import dataProviders from './service/dataproviders';

import UserList1, from './users1';

import UserList2, from './users2';

const App = () => (

render(

<Admin dataProvider={dataProviders}>

<Resource name="users1" list={UserList1} />

<Resource name="users2" list={UserList2} />

</Admin>,

document.getElementById('root'),

)

);

export default App;

我有dataproviders.js文件:

import simpleRestProvider from 'ra-data-simple-rest';

const dataProviders = [

{ dataProvider: simpleRestProvider('http://path.to.foo.api1'), resources: ['users1'] },

{ dataProvider: simpleRestProvider('http://path.to.foo.api2'), resources: ['users2'] },

];

export default (type, resource, params) => {

const dataProvider = dataProviders.find(dp => dp.resources.includes(resource));

return dataProvider(type, resource, params);

};

这是正确的方法吗?

回答:

您不是在调用dataProvider找到的对象,而是在从数组中调用映射对象。您可以这样解决:

import simpleRestProvider from 'ra-data-simple-rest';

const dataProviders = [

{ dataProvider: simpleRestProvider('http://path.to.foo.api1'), resources: ['users1'] },

{ dataProvider: simpleRestProvider('http://path.to.foo.api2'), resources: ['users2'] },

];

export default (type, resource, params) => {

const dataProviderMapping = dataProviders.find(dp => dp.resources.includes(resource));

return dataProviderMapping.dataProvider(type, resource, params);

};

以上是 如何在react-admin中拥有多个dataProviders的正确方法? 的全部内容, 来源链接: utcz.com/qa/409654.html

回到顶部