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

时间:2018-07-17 19:15:11

标签: reactjs redux react-redux react-admin

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

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

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组件的道具时,我可以看到admin对象确实存在,但其中不包含“ 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

1 个答案:

答案 0 :(得分:2)

您需要手动“ registerResource”。

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

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


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