【发布时间】:2019-01-02 17:40:38
【问题描述】:
我对 React 生命周期有些困惑,特别是 componentDidMount 如何与 redux 交互。
这是场景:我有一个连接到 redux 存储的组件,我不知道是否应该在更新 redux 存储并且连接组件中的 props 更改时再次调用 componentDidMount。现在,每次道具更改时都会重新安装一个组件。但是,我有另一个单独的连接组件,当它的道具改变时不会重新安装。所以现在我因为这种不一致的行为而迷路了。
我环顾四周,但有些答案像 Why is componentDidMount not being called when I re-render? 和 ComponentDidMount called multiple times 让我很困惑。
这是一个在 props 更改时没有正确更新的代码示例
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from "redux";
import PropTypes from 'prop-types';
/* material UI */
import Collapse from '@material-ui/core/Collapse';
import Divider from '@material-ui/core/Divider';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Tooltip from '@material-ui/core/Tooltip';
/* Icons */
import {Copy,FolderMinus,FolderPlus} from 'react-feather';
/* Our Components */
import { history } from '../../redux/store/index';
import { getClusters, setCluster, toggleClusters } from '../../redux/actions/ClusterActions';
class SideClustersList extends Component{
constructor(props){
super(props);
}
componentDidMount(){
/* get a list of clusters based on the apiurl */
console.log("props get clusters")
console.log(this.props.base_url)
this.props.getClusters(this.props.base_url);
}
setCurrentCluster(cluster) {
this.props.setCluster(this.props.base_url, cluster)
// Switch tabs to display selected cluster
history.push("/clusters")
}
render(){
const { classes,clusters, open } = this.props;
return (
<React.Fragment>
<ListItem button className={classes.nested} onClick={this.handleClick}>
<Tooltip id="tooltip-bottom" title="Clusters" placement="top">
<ListItemIcon >
{open ? <FolderMinus/>:<FolderPlus/>}
</ListItemIcon>
</Tooltip>
<ListItemText primary="Clusters" /></ListItem>
<Collapse in={open} timeout="auto" unmountOnExit>
<Divider />
<List component="div" disablePadding>
{clusters ? (
clusters.map(cls =>(
<ListItem button key={cls.clusterName} component='a'
/*href={'/clusters/'+this.props.accounts[0]['account_name']+'/'+cls.clusterName}*/
onClick={() => this.setCurrentCluster(cls.clusterName)}>
<Tooltip id="tooltip-top" title={cls.clusterName} placement="top">
<ListItemIcon>
<Copy />
</ListItemIcon>
</Tooltip>
<ListItemText primary={cls.clusterName} />
</ListItem>
))): null}
</List>
</Collapse>
</React.Fragment>
)
}
handleClick = () => {
this.props.toggleClusters(!this.props.open)
};
}
SideClustersList.propTypes = {
getClusters: PropTypes.func.isRequired
};
const mapStateToProps = state => ({
account_name: state.accounts.item.account_name,
base_url: state.accounts.item.apiurl,
cluster: state.clusters.item,
clusters: state.clusters.items,
open: state.clusters.open,
});
function mapDispatchToProps(dispatch) {
return bindActionCreators({getClusters, setCluster, toggleClusters}, dispatch)
}
export default connect(
mapStateToProps, mapDispatchToProps
)(SideClustersList);
【问题讨论】:
-
你能发布例子吗?最好谈谈具体情况。
-
componentDidMount在生命周期中只被调用一次。如果您想在 Redux 更改后获取值,您需要使用componentDidUpdate。
标签: reactjs redux react-redux