如何从componentDIdMount中的redux获取数据?

时间:2018-09-09 14:18:09

标签: reactjs redux react-redux

在控制台中什么也没有,哪里可能出错? 需要获取this.props.about并检查是否为空。

reducer.js

 export default function details(state = initialState, action) {
  switch(action.type) {
    case DETAILS_SUCCESS:
  return { ...state, details: action.payload, error: '' };...

Container.js

class HeaderContainer extends Component {
  render() {
      const { details } = this.props, { deTails } = this.props.HeaderAction;
      return <div><Header deTails={deTails} about={details.details} error={details.error} /></div>
  }
}

function mapStateToProps(state) {
  return {
    details: state.details,
  }
}

function mapDispatchToProps(dispatch) {
  return {
    HeaderAction: bindActionCreators(HeaderAction, dispatch),
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(HeaderContainer);

Component.js

componentDidMount() {
  console.log(this.props.about);
}

1 个答案:

答案 0 :(得分:0)

您不会像componentDidMount中的道具那样收到更新的状态,而是可以使用:


componentWillReceiveProps(nextProps){  // this is UN_SAFE 

}

 static getDerivedStateFromProps(nextProps, prevState) { // this is recommended
 }