将 react-admin 添加到现有项目 - resourceState 未定义

Adding react-admin to existing project - resourceState is undefined

我正在按照 https://marmelab.com/react-admin/CustomApp.html 上的指南将 react-admin 合并到现有的 react-redux 项目中。

我已将 admin、i18n 和路由器状态对象注入到通用列表组件中。但是,在渲染组件时,出现以下错误:

TypeError: resourceState is undefined
mapStateToProps
node_modules/ra-core/lib/controller/ListController.js:413
410 | 
411 | return {
412 |     query: getQuery(props),
> 413 |     params: resourceState.list.params,
414 |     ids: resourceState.list.ids,
415 |     selectedIds: resourceState.list.selectedIds,
416 |     total: resourceState.list.total,

重要的一点是,当我 console.log 我的组件的道具时,我可以看到管理对象确实存在但它不包含任何 "resources" 对象。如何将资源手动加载到我商店的管理员中?

组件的智能容器:

class MyContainer extends Component {
  render() {
    return(
      <Switch>
        <Route exact path='/admin/stuff/list' hasCreate render=
          {() => <MyList resource='myStuff' {...this.props}/>}/>
      </Switch>
    )
  }
}

const mapStateToProps = (state) =>{
  return {
    admin: state.admin,
    i18n: state.i18n,
    form: state.form
  }
}

最后,MyList 组件:

import React, { Component } from 'react'
import { List, Datagrid } from 'react-admin'

class MyList extends Component {
  constructor(props) {
    super(props)
  }

  render() {
    return(
      <List {...this.props}>
        <Datagrid>
        </Datagrid>
      </List>
    )
  }
}

export default MyList

您需要手动"registerResource"。

因此,在您创建商店后,请执行以下操作:

import * as reactAdmin from "ra-core";
...
...
const store = createStore....


store.dispatch(reactAdmin.registerResource({
  name: "myStuff",
  hasList: true,
  hasEdit: false,
  hasShow: false,
  hasCreate: false
}));