Reducer在初始化期间返回未定义

时间:2019-12-28 13:52:55

标签: react-native react-redux immer.js redux-reducers

很长一段时间以来,我一直对此坚持不懈,到目前为止,没有任何在线支持对此有所帮助。 我正在尝试在我的React-Native应用程序中使用沉浸式实现不变性。但是reducer部分给出了一个错误,指出在初始化过程中未定义返回的reducer“ count”。 我的减计数器看起来像这样-

import produce from "immer";
import {
    INCREMENT,
    DECREMENT,
} from '../action/index.js';

const INITIAL_STATE = {
    count: 0,
};

const countReducer = (state, action) =>
    produce(state, draft => {
        switch (action.type) {
            case INCREMENT: {
                draft = draft + 1;
                break;
            }
            case DECREMENT: {
                draft = draft - 1;
                break;
            }
        }
});

export default countReducer;

我的rootReducer是-

import {combineReducers} from 'redux';
import countReducer from './countReducer.js';
const rootReducer = combineReducers({
  count: countReducer,
});
export default rootReducer;

我该如何解决?

1 个答案:

答案 0 :(得分:0)

从外观上看,您并未在代码中的任何地方使用class SimpleLRU { public: SimpleLRU(int cap) : cap_(cap) {} int get(int key) { auto it = cache_.find(key); if (it != cache_.end()) { int val = it->second->second; <--- line 13 list_.erase(it->second); list_.emplace_front(key, val); cache_.emplace(key, list_.begin()); return val; } return -1; } void put(int key, int value) { auto it = cache_.find(key); if (it != cache_.end()) { list_.erase(it->second); } else if (cache_.size() == cap_) { cache_.erase(list_.back().first); list_.pop_back(); } list_.emplace_front(key, value); cache_.emplace(key, list_.begin()); } private: int cap_; std::list<std::pair<int, int>> list_; std::unordered_map<int, std::list<std::pair<int, int>>::iterator> cache_; };

我没用过沉浸式,但是通常你会这样初始化你的状态

==1903037==ERROR: AddressSanitizer: heap-use-after-free on address 0x60300000eff4 at pc 0x56488c0f9ff2 bp 0x7ffebf9a0ba0 sp 0x7ffebf9a0b98
READ of size 4 at 0x60300000eff4 thread T0
    #0 0x56488c0f9ff1 in SimpleLRU::get(int) test.cc:13

所以我想你想做些类似的事情:

INITIAL_STATE