javascript - React 开发工具未显示更新状态
问题描述
我有一个非常简单的 react 应用程序,它使用 redux 进行状态管理,但是状态没有在 react 开发工具中正确更新,即使状态从容器正确传递到组件也是如此。
应用程序.js
import './App.css';
import TableTopContainer from './components/TableTop/TableTopContainer';
import { Provider } from "react-redux";
import reducer from './reducers';
import { createStore, applyMiddleware } from 'redux'
import thunk from "redux-thunk"
import { composeWithDevTools } from 'redux-devtools-extension';
function App() {
const store = createStore(
reducer,
composeWithDevTools(applyMiddleware(thunk))
);
return (
<div className="App">
<header className="App-header">
<Provider store={store}>
<TableTopContainer />
</Provider>
</header>
</div>
);
}
export default App;
减速器deck.js
import { suits } from '../common/suits';
import { displayNames } from '../common/displayNames';
import { valueMap } from '../common/valueMap';
/**
* Actions
*/
export const GENERATE_DECK_COMPLETE = 'deck/GENERATE_DECK_COMPLETE';
/**
* Initial State
*/
const initialState = {
deck: [],
selectedCards: []
};
/**
* Reducer function
* @param {*} state
* @param {*} action
*/
export default function reducer(state = initialState, action = {}) {
const { type, payload } = action;
switch(type) {
case GENERATE_DECK_COMPLETE:
return {
...state,
deck: [...payload, ...state.deck]
};
default:
return state;
}
}
/**
* Create a 52 card deck
*/
export const generateDeck = () => (dispatch) => {
const deck = [];
for (const suit of suits) {
for (const displayName of displayNames) {
deck.push(createCard(suit, displayName));
}
}
const shuffledDeck = deck.sort(() => Math.random() - 0.5);
dispatch({
type: GENERATE_DECK_COMPLETE,
payload: shuffledDeck
});
}
容器
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import TableTop from './TableTop';
import {
generateDeck,
handleCardSelect,
handleCardDeselect
} from '../../reducers/deck';
// here the state gets passed to the component correctly to be rendered,
// but it's not showing in the react dev tools
const mapStateToProps = createSelector(
(state) => state.deck,
(deck) => ({ ...deck })
);
const mapDispatchToProps = (dispatch) => ({
generateDeck: () => dispatch(generateDeck()),
});
export default connect(mapStateToProps, mapDispatchToProps)(TableTop);
解决方案
推荐阅读
- sapui5 - Sap Fiori OData 嵌套展开
- angular - 如何在当前版本的 Angular 12 中替换“@angular/material”?
- flutter - 加载网站时获取所有请求
- flutter - 插件 cloud_firestore 和 firebase_auth 阻止与 android Emulator 同步
- google-bigquery - 在 BigQuery 中,有没有办法检查文本列值是否是有效的 JSON 字符串?
- java - 在 Java 中使用 FileInputStream 从文件中读取文本时出现问题
- r - * 与 .* 在 R 中使用正则表达式
- flutter - 1 QuerySnapshot 在 Flutter Web 应用程序中返回 null 但在 Android 控制台中给出结果
- appinsights - 为什么我的日志消息总是有顶级操作名称?
- css - 为什么使用 React Native 会模糊图像?