gpt4 book ai didi

javascript - Redux 存储和 PouchDB 未与 redux-pouchdb 同步

转载 作者:行者123 更新时间:2023-11-28 03:54:41 26 4
gpt4 key购买 nike

我正在尝试将我的 redux 状态同步到 PouchDB 实例,但遇到了问题。

正如日志中所示,在操作发生后,我的 redux 状态既没有同步到 PouchDB,PouchDB 文档也没有在更新时同步到 redux 状态。

我尝试实现redux-pouchdb,因为它是在 jrzerr/react-redux-pouchdb 中实现的但我既没有收到错误,也没有同步工作。

这是我正在使用的代码:

Reducer(modules/mainbase.js):

import { persistentReducer } from 'redux-pouchdb';
export const INPUT_CHANGED = 'mainbase/INPUT_CHANGED'

const initialState = {}

function mainbase (state = initialState, action) {
switch (action.type) {
case INPUT_CHANGED:
state[action.key] = action.value;
return {...state}
default:
return state;
}
}

export default persistentReducer(mainbase);

export const inputChanged = (key, value) => {
return dispatch => {
dispatch({
type: INPUT_CHANGED,
key: key,
value: value
})
}
}

根 reducer (modules/index.js):

import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
import mainbase from './mainbase'

export default combineReducers({
router: routerReducer,
mainbase
})

商店(store.js):

import { createStore, applyMiddleware, compose } from 'redux'
import { routerMiddleware } from 'react-router-redux'
import thunk from 'redux-thunk'
import createHistory from 'history/createBrowserHistory'
import rootReducer from './modules'
import { persistentStore } from 'redux-pouchdb'
import PouchDB from 'pouchdb'

export const history = createHistory()

const db = new PouchDB('testproject')

const initialState = {}

const enhancers = [
persistentStore(db)
]

const middleware = [
thunk,
routerMiddleware(history)
]

const composedEnhancers = compose(
applyMiddleware(...middleware),
...enhancers
)

const store = createStore(
rootReducer,
initialState,
composedEnhancers
)

export default store

非常感谢您的帮助。

最佳答案

在主库中,您直接更新状态。这通常效果不佳。您应该首先创建当前状态的副本,更新该副本并将更新后的副本作为新状态返回。

关于javascript - Redux 存储和 PouchDB 未与 redux-pouchdb 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47655111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com