我正在使用react native Ignite Andross Boilerplate,并且尝试保持导航状态,以便当用户关闭/打开应用程序时,它会返回到先前的屏幕。
例如:用户填写了一份注册表单,然后关闭了该应用程序,稍后再回来以完成他停下的地方。
由于Redux持久性是与Ignite一起提供的,我可以将导航reducer添加到持久性白名单中,然后让redux持久化其工作,将我的导航状态保存在本地存储中。
所以我做到了:
// App/Config/ReduxPersist.js
import immutablePersistenceTransform from '../Services/ImmutablePersistenceTransform'
import { AsyncStorage } from 'react-native'
// More info here: https://shift.infinite.red/shipping-persistant-reducers-7341691232b1
const REDUX_PERSIST = {
active: true,
reducerVersion: '1.1',
storeConfig: {
key: 'primary',
storage: AsyncStorage,
// Reducer keys that you do NOT want stored to persistence here.
blacklist: ['login', 'search'],
// Optionally, just specify the keys you DO want stored to persistence.
// An empty array means 'don't store any reducers' -> infinitered/ignite#409
whitelist: ['nav'],
transforms: [immutablePersistenceTransform]
}
}
export default REDUX_PERSIST
问题是...我得到此错误作为结果:
以某种方式,我意识到immutablePersistenceTransform
是应该受到指责的人。我怎么知道的?好吧...我没有!我只是看到不变的唯一相关就是转换,我对此进行了评论。
因此,当我的代码如下时,它将起作用:
// App/Config/ReduxPersist.js
import immutablePersistenceTransform from '../Services/ImmutablePersistenceTransform'
import { AsyncStorage } from 'react-native'
// More info here: https://shift.infinite.red/shipping-persistant-reducers-7341691232b1
const REDUX_PERSIST = {
active: true,
reducerVersion: '1.1',
storeConfig: {
key: 'primary',
storage: AsyncStorage,
// Reducer keys that you do NOT want stored to persistence here.
blacklist: ['login', 'search'],
// Optionally, just specify the keys you DO want stored to persistence.
// An empty array means 'don't store any reducers' -> infinitered/ignite#409
whitelist: ['nav'],
//transforms: [immutablePersistenceTransform]
}
}
export default REDUX_PERSIST
// App / Services / ImmutablePersistenceTransform.js
为什么会出现此错误?评论转换是最好的解决方法吗?任何帮助都将受到欢迎!!!
此外,immutablePersistenceTransform
如下:
import R from 'ramda'
import Immutable from 'seamless-immutable'
// is this object already Immutable?
const isImmutable = R.has('asMutable')
// change this Immutable object into a JS object
const convertToJs = (state) => state.asMutable({deep: true})
// optionally convert this object into a JS object if it is Immutable
const fromImmutable = R.when(isImmutable, convertToJs)
// convert this JS object into an Immutable object
const toImmutable = (raw) => Immutable(raw)
// the transform interface that redux-persist is expecting
export default {
out: (state) => {
// console.log({ retrieving: state })
return toImmutable(state)
},
in: (raw) => {
// console.log({ storing: raw })
return fromImmutable(raw)
}
}
有帮助吗?
答案 0 :(得分:-1)
伙计,我认为“ nav”不是不可变的数据结构。这就是为什么抛出此错误