我在使用 vite 的反应项目中使用 redux-orm。但是,只要有 useSelector 调用,我就会收到此错误:
useSelector.js:38 Uncaught TypeError: Cannot read property 'session' of undefined
at memoize.js:185
at index.jsx:68
at useSelectorWithStoreAndSubscription (useSelector.js:29)
at useSelector2 (useSelector.js:106)
at App (index.jsx:68)
at renderWithHooks (react-dom.development.js:14985)
at mountIndeterminateComponent (react-dom.development.js:17811)
at beginWork (react-dom.development.js:19049)
at HTMLUnknownElement.callCallback2 (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
[memoize.js] 来自 redux-orm 节点模块文件夹。
index.js 线:68
const formDefinitions = useSelector(state => fullFormDefinitionSelector(state)); // ! This is causing an error
formDefinition 选择器:
import {createSelector} from 'redux-orm';
import orm from '../schema/models';
// Selector to return fully populated datasources.
export const fullFormDefinitionSelector = createSelector(
orm,
session => {
return session.FormDefinition.all()
.toModelArray()
.map(fd => {
return {
...fd.ref,
schema: fd.schema.toRefArray(),
};
});
},
);
我不认为这个选择器有什么问题,因为所有的选择器都给出了相同的错误,所以可能会丢失一些配置。
模型.js
/**
* Redux-orm Models defined here
*/
import {Model, ORM} from 'redux-orm';
import {Datasource, Field, DatasourceTag} from './Datasource';
import {Query} from './Query';
import {FormDefinition, BaseSchema, GridSchema, KPISchema, Pagination, UserFilter, Sorting, ControlDatasource, DatasourceFilter, GridColumn, KPIColumn, KPIObject} from './FormDefinition';
import {POPULATE_USER_SESSION} from 'containers/App/constants';
export class User extends Model {
static parse(userData) {
return this.upsert(userData);
}
static reducer(action, User, session) {
switch (action.type) {
case POPULATE_USER_SESSION:
User.parse(action.payload);
break;
}
}
static get modelName() {
return 'User';
}
}
// Create an ORM instance and hook up the Post and Comment models
export const orm = new ORM({
stateSelector: state => state.orm,
});
orm.register(Datasource, Field, DatasourceTag, User, Query, FormDefinition, BaseSchema, GridSchema, KPISchema, Pagination, UserFilter, Sorting, ControlDatasource, DatasourceFilter, GridColumn, KPIColumn, KPIObject);
export default orm;