gpt4 book ai didi

javascript - React-redux 存储状态和 mapStateToProps 状态不同的原因是什么?

转载 作者:行者123 更新时间:2023-11-29 20:40:50 26 4
gpt4 key购买 nike

我遇到的情况是,当我调度一个 Action 时,store 状态正在改变,但我连接的组件 mapStateToProps 回调状态没有反射(reflect) store 中的新变化。

这可能是什么原因?

configureStore.js

export default () => {
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const store = createStore(
combineReducers({
concepts: conceptReducer,
conceptModal: conceptModalReducer,
cards: cardReducer,
cardModal: cardModalReducer,
}),
composeEnhancers(applyMiddleware(queryApi))
);

store.subscribe(() => {
console.log('store notification:', store.getState());
})

return store;
};

Reducer.js

case "UPDATE_CONCEPT_SUCCESS": {
let concepts = [...state.data].map((concept) => {
if (concept.id === action.id) {
return { ...concept, ...action.concept };
}
return concept;
});

/* Even with return below, mapStateToProps state parameter still returns the old state */
// return {'data': [{id:1},{id:2}]}
return { ...state, pending: false, lastErrorCode: null, lastErrorMessage: null, data: concepts };
break;
}

CardModal.js

export default connect((state, p) => (() => {
// the console.log below returns the old state
console.log('connect called', state);

return ({
isThemeDark: state.common.isThemeDark,

allConceptsData: allConcepts(state.concepts.data),
conceptsData: allConceptsForTopicId(state.concepts.data, state.cardModal.topicId),
})
}))(CardModal);

最佳答案

您的 mapStateToProps 正在返回一个函数,而不是新状态。

试试这个:

export default connect((state, p) => {

// the console.log below returns the old state
console.log('connect called', state);

return {
isThemeDark: state.common.isThemeDark,

allConceptsData: allConcepts(state.concepts.data),
conceptsData: allConceptsForTopicId(state.concepts.data, state.cardModal.topicId),
};
}))(CardModal);

关于javascript - React-redux 存储状态和 mapStateToProps 状态不同的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55580924/

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