我正在使用redux-orm來創建用於規範化和非規範化的模型。我發現,當我創建一個模式,我得到的錯誤:在redux-orm模式實例上使用reducer方法
Uncaught Error: Schema has been renamed to ORM. Please import ORM instead of Schema from Redux-ORM
我用兩個0.90-rc1
這是默認安裝以及`0.8.4
這段代碼運行時版本:
import { Schema } from 'redux-orm'
import Todo from './Todo'
import Tag from './Tag'
import User from './User'
const schema = new Schema()
schema.register(Todo, Tag, User)
export default schema
但是我發現redux-orm中的模式文檔和代碼仍然存在。
如果我從
import { Schema } from 'redux-orm'
切換到
import { ORM as Schema } from 'redux-orm'
代碼的作品,但我得到一個錯誤指定reducer
方法沒有在這裏定義:
import { schema } from './models'
console.log(schema)
const rootReducer = combineReducers({
orm: schema.reducer(),
selectedUserId: selectedUserIdReducer
})
大多數代碼是基於primer here
個我的模型是這樣的:
ValidatingModel.js
import { PropTypes } from 'react'
import { Model } from 'redux-orm'
import propTypesMixin from 'redux-orm-proptypes'
const ValidatingModel = propTypesMixin(Model)
export default ValidatingModel
Todo.js
import { fk, many } from 'redux-orm'
import { PropTypes } from 'react'
import ValidatingModel from './ValidatingModel'
import User from './User'
import Tag from './Tag'
import { CREATE_TODO, MARK_DONE, DELETE_TODO, ADD_TAG_TO_TODO, REMOVE_TAG_FROM_TODO } from '../actionTypes'
export default class Todo extends ValidatingModel {
static reducer (state, action, Todo, session) {
const { payload, type } = action
switch (type) {
case CREATE_TODO:
const tagIds = payload.tags.split(',').map(str => str.trim())
const props = Object.assign({}, payload, { tags: tagIds })
Todo.create(props)
break
case MARK_DONE:
Todo.withId(payload).set({ done: true })
break
case DELETE_TODO:
Todo.withId(payload).delete()
break
case ADD_TAG_TO_TODO:
Todo.withId(payload.todo).tags.add(payload.tag)
break
case REMOVE_TAG_FROM_TODO:
Todo.withId(payload.todo).tags.remove(payload.tag)
break
}
}
}
Todo.modelName = 'Todo'
Todo.propTypes = {
id: PropTypes.number,
text: PropTypes.string.isRequired,
done: PropTypes.bool.isRequired,
user: PropTypes.oneOf([PropTypes.instanceOf(User), PropTypes.number]),
tags: PropTypes.arrayOf(PropTypes.oneOf([
PropTypes.number,
PropTypes.instanceOf(Tag)
]))
}
Todo.defaultProps = {
done: false
}
Todo.fields = {
user: fk('User', 'todos'),
tags: many('Tag', 'todos')
}
Tag.js
import ValidatingModel from './ValidatingModel'
import { PropTypes } from 'react'
import { CREATE_TODO, ADD_TAG_TO_TODO } from '../actionTypes'
export default class Tag extends ValidatingModel {
static reducer (state, action, Tag, session) {
const { payload, type } = action
switch (type) {
case CREATE_TODO:
const tags = payload.tags.split(',')
const trimmed = tags.map(name => name.trim())
trimmed.forEach(name => Tag.create(name))
break
case ADD_TAG_TO_TODO:
if (!Tag.filter({ name: payload.tag }).exists()) {
Tag.create({ name: payload.tag })
}
break
}
}
}
Tag.modelName = 'Tag'
Tag.backend = {
idAttribute: 'name'
}
Tag.propTypes = {
name: PropTypes.string
}
這是用戶模型,我添加了一個空操作減速看到@squiroid原來的答案
import ValidatingModel from './ValidatingModel'
import { PropTypes } from 'react'
export default class User extends ValidatingModel {
static reducer (state, action, User, session) {
return state
}
}
User.modelName = 'User'
User.propTypes = {
id: PropTypes.number,
name: PropTypes.string
}
我將模型添加到問題中,您可以看到我已經爲每個模型定義了一個縮減器。 – vamsiampolu