如何解决React Redux不会更新类组件的状态
我有一个组件,如果使用redux更改了另一个组件的状态,则应该更新该组件,但事实并非如此。
在+-----------+---------------+------------+----------+----------+---------+
| Host Name | Serial Number | IP Address | Priority | Category | Version |
+-----------+---------------+------------+----------+----------+---------+
| SwitchA | 1230 | 1.1.1.1 | 1 | Switch | X |
+-----------+---------------+------------+----------+----------+---------+
| SwitchA | 1231 | 1.1.1.1 | 1 | Switch | X |
+-----------+---------------+------------+----------+----------+---------+
| SwitchB | 1240 | 1.1.1.2 | 2 | Switch | Y |
+-----------+---------------+------------+----------+----------+---------+
内部,通过redux操作返回了正确的值。
+-----------+---------------+------------+----------+----------+-------------+-------+
| Host Name | Serial Number | IP Address | Priority | Category | Coverage | Price |
+-----------+---------------+------------+----------+----------+-------------+-------+
| SwitchA | 1230 | 1.1.1.1 | 1 | Switch | Not Covered | 100 |
+-----------+---------------+------------+----------+----------+-------------+-------+
| SwitchA | 1231 | 1.1.1.1 | 1 | Switch | Covered | 300 |
+-----------+---------------+------------+----------+----------+-------------+-------+
| SwitchB | 1240 | 1.1.1.2 | 2 | Switch | Not Covered | 200 |
+-----------+---------------+------------+----------+----------+-------------+-------+
在另一个组件内部调度-执行该组件时,第一个组件的状态+-----------+---------------+------------+----------+----------+---------+-------------+-------+
| Host Name | Serial Number | IP Address | Priority | Category | Version | Coverage | Price |
+-----------+---------------+------------+----------+----------+---------+-------------+-------+
| SwitchA | 1230 | 1.1.1.1 | 1 | Switch | X | Not Covered | 100 |
+-----------+---------------+------------+----------+----------+---------+-------------+-------+
| SwitchA | 1231 | 1.1.1.1 | 1 | Switch | X | Covered | 300 |
+-----------+---------------+------------+----------+----------+---------+-------------+-------+
| SwitchB | 1240 | 1.1.1.2 | 2 | Switch | Y | Not Covered | 200 |
+-----------+---------------+------------+----------+----------+---------+-------------+-------+
| SwitchD | 1250 | 1.2.2.2 | 1 | Switch | | | |
+-----------+---------------+------------+----------+----------+---------+-------------+-------+
| SwitchE | 1260 | 1.3.3.3 | 2 | Switch | | | |
+-----------+---------------+------------+----------+----------+---------+-------------+-------+
将变为 Source.Name_x Priority Item Type_x Category Product Family Product ID_x Software Type_x OS Version_x Suggested Version 1_x Host Name IP Address_x Serial Number Source.Name_y Product ID_y Software Type_y OS Version_y Current Milestone_x Suggested Version 1_y Suggested Version 2 Suggested Version 3 IP Address_y SW End of Life SW End of Sale URL_x Source.Name_x IP Address_x Item Type_y Product ID_x Current Milestone_y Hardware Lifecycle Status Replacement PID Replacement PID Info Replacement PID Price Replacement PID Price Discount Replacement PID Service Level Replacement PID Service Price Current PID Service Price Replacement PID Service Price Discount HW End of Life HW End of Sale URL_y Source.Name_y Item Type Product ID_y IP Address_y Coverage Status Contract Status Contract Number Coverage Start Date Coverage End Date SLA type
inventory_30_Jun_15_19_35.xlsx 3 Chassis Switches Cisco Catalyst 2960-X Series Switches WS-C2960X-24PS-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-IDF5-A1 10.1.1.8 XXXXX software_02_Jul_07_54_15.xlsx WS-C2960X-24PS-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.8 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_06_49.xlsx Chassis WS-C2960X-24PS-L 10.1.1.8 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
inventory_30_Jun_15_19_35.xlsx 3 Chassis Switches Cisco Catalyst 2960-X Series Switches WS-C2960X-48LPS-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-IDF6-A1 10.1.1.9 YYYYY software_02_Jul_07_54_15.xlsx WS-C2960X-48LPS-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.9 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_06_49.xlsx Chassis WS-C2960X-48LPS-L 10.1.1.9 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
inventory_30_Jun_15_19_35.xlsx 3 Chassis Switches Cisco Catalyst 2960-X Series Switches WS-C2960X-24PS-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-IDF7-A1 10.1.1.11 ZZZZZZ software_02_Jul_07_54_15.xlsx WS-C2960X-24PS-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.11 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_06_49.xlsx Chassis WS-C2960X-24PS-L 10.1.1.11 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
inventory_30_Jun_15_19_35.xlsx 3 Chassis Switches Cisco Catalyst 2960-X Series Switches WS-C2960X-24PS-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-IDF8-A1 10.1.1.12 QQQQQ software_02_Jul_07_54_15.xlsx WS-C2960X-24PS-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.12 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_06_49.xlsx Chassis WS-C2960X-24PS-L 10.1.1.12 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
inventory_30_Jun_15_19_35.xlsx 3 Chassis Switches Cisco Catalyst 2960-X Series Switches WS-C2960X-48LPS-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-IDF9-A1 10.1.1.13 WWWWW software_02_Jul_07_54_15.xlsx WS-C2960X-48LPS-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.13 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_06_49.xlsx Chassis WS-C2960X-48LPS-L 10.1.1.13 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
inventory_30_Jun_15_30_08.xlsx 3 Chassis Switches Cisco Catalyst 2960-C Series Switches WS-C2960C-12PC-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-MGK-A1 10.1.1.39 EEEEEE software_02_Jul_08_14_40.xlsx WS-C2960C-12PC-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.39 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html hardware_02_Jul_07_25_04.xlsx 10.1.1.39 Chassis WS-C2960C-12PC-L EoL Date Announced EOL in more than 24 months WS-C2960L-16PS-LL None 920.7 50 PSUT 215.16 122.76 0 2025-10-31 2020-10-30 https://www.cisco.com/c/en/us/products/switches/catalyst-2960-c-series-switches/eos-eol-notice-c51-743071.html coverage_24_Jul_10_37_26.xlsx Chassis WS-C2960C-12PC-L 10.1.1.39 Uncovered with ELLW No Contract No Contract NaT NaT None
inventory_30_Jun_15_19_35.xlsx 3 Chassis Switches Cisco Catalyst 2960-X Series Switches WS-C2960X-48LPS-L IOS 15.2(4)E6 15.2(7)E2 SWITCH-SRVROOM-A1 10.1.1.2 RRRRRR software_02_Jul_07_54_15.xlsx WS-C2960X-48LPS-L IOS 15.2(4)E6 End of Vulnerability Support 15.2(7)E2 <NA> <NA> 10.1.1.2 2023-04-30 2018-05-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-4500-series-switches/eos-eol-notice-c51-739919.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_06_49.xlsx Chassis WS-C2960X-48LPS-L 10.1.1.2 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
inventory_30_Jun_15_20_39.xlsx 3 Chassis Switches Cisco Catalyst 3850 Series Switches WS-C3850-24P-S IOS-XE 16.3.7 16.9.5 SWITCH-SRVROOM-C1 10.2.1.254 TTTTTT software_02_Jul_07_54_33.xlsx WS-C3850-24P-S IOS-XE 16.3.7 End of Engineering 16.9.5 <NA> <NA> 10.2.1.3 2023-07-31 2018-08-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-3850-series-switches/eos-eol-notice-c51-740255.html software_02_Jul_07_02_48.xlsx 10.2.1.254 <NA> <NA> End of Engineering <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> 2023-07-31 2018-08-01 https://www.cisco.com/c/en/us/products/collateral/switches/catalyst-3850-series-switches/eos-eol-notice-c51-740255.html coverage_24_Jul_10_07_28.xlsx Chassis WS-C3850-24P-S 10.2.1.254 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
software_30_Jun_15_21_13.xlsx 1 <NA> Security Cisco ASA 5500-X with FirePOWER Services <NA> ASA 9.7(1)4 9.12.3 Interim SRVROOM-FW2.umbrellacorp.com 10.60.127.19 YYYYYY software_02_Jul_07_55_54.xlsx ASA5506-K9 ASA 9.7(1)4 End of Engineering 9.12.3 Interim 9.8.4 Interim <NA> 10.1.122.9 2022-08-31 2017-08-25 http://www.cisco.com/c/en/us/products/collateral/security/asa-firepower-services/eos-eol-notice-c51-738646.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_07_48.xlsx Chassis ASA5506-K9 10.60.127.19 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
software_30_Jun_15_21_13.xlsx 1 <NA> Security Cisco ASA 5500-X with FirePOWER Services <NA> ASA 9.7(1)4 9.12.3 Interim FW2.umbrellacorp.com 10.60.127.18 GGGGGGG software_02_Jul_07_55_54.xlsx ASA5506-K9 ASA 9.7(1)4 End of Engineering 9.12.3 Interim 9.8.4 Interim <NA> 10.1.122.8 2022-08-31 2017-08-25 http://www.cisco.com/c/en/us/products/collateral/security/asa-firepower-services/eos-eol-notice-c51-738646.html <NA> <NA> <NA> <NA> <NA> <NA> <NA> None NaN <NA> <NA> NaN NaN <NA> NaT NaT <NA> coverage_24_Jul_10_07_48.xlsx Chassis ASA5506-K9 10.60.127.18 Covered - Non-IBM Covered - Non-IBM Covered - Non-IBM NaT NaT None
:
mapStateToProps
Actions.js
import React,{Component} from "react";
import './DashboardContent.scss';
import * as entries from '../../../assets/demo.json';
import CategoryHeader from "./CategoryHeader/CategoryHeader";
import NoContent from "../../../shared/NoContent/NoContent";
import UnsortedList from "./UnsortedList/UnsortedList";
import {connect} from "react-redux";
class DashboardContent extends Component {
state = {
activeCategory: this.props.activeCategory
};
componentDidUpdate(prevProps,prevState,snapshot) {
console.log(this.props.activeCategory)
}
render() {
return (
<div>
...
</div>
)
}
}
const mapStateToProps = state => {
console.log(state);
return {
activeCategory: state.category
}
};
export default connect(mapStateToProps)(DashboardContent);
Reducer.js
activeCategory
解决方法
您的reducer应该返回更新的状态,而不是返回action.category atm。
export const reducer = (state = initialState,action) => {
console.log('reducer',state,action);
if (action.type === 'CHANGE_CATEGORY') {
return {...state,category:action.category};
}
return state;
};
,
将其添加到主类时会发生什么?(类DashboardContent扩展了Component) 我相信您在尝试访问this.props.activeCategory时需要这样做。尝试添加此代码段。
constructor(props) {
super(props);
this.state = this.props.activeCategory;
}
您可以参考以下答案:What's the difference between "super()" and "super(props)" in React when using es6 classes?
,将组件连接到redux时,第二个参数是dispatchToProps,这是在组件内部分派操作的唯一方法
export default connect(mapStateToProps,{action1,action2,})(DashboardContent);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。