我应该在生命周期中的什么地方响应Redux

时间:2019-11-04 06:38:10

标签: reactjs react-redux redux-thunk

我的应用程序遇到问题,我想根据已登录的用户ID从用户集合中检索一个数组Lessons。 我正在使用redux thunk,jwt auth

我试图在componentDidMount中分派我的getMyLessons,但是它将this.props.auth.user用作null参数,但是如果我在render()上使用它,它会加载(它会冻结但会加载,哈哈),所以我认为lycecicle问题

class DashboardSidebar extends Component {
    componentDidMount(){
        this.props.getMyLessons(this.props.auth.user._id);
    }     

    static propTypes = {
        auth: PropTypes.object.isRequired,
        lesson: PropTypes.object.isRequired,
        getMyLessons: PropTypes.func.isRequired
    }

const mapStateToProps = (state) => ({    
    auth: state.auth,
    lesson: state.lesson
});

export default connect(mapStateToProps,{ getMyLessons })(DashboardSidebar);

1 个答案:

答案 0 :(得分:0)

您可以在componentDidUpdate中进行操作:

componentDidUpdate(prevProps) {
  const {user} = this.props.auth;
  if (user && user._id && user._id !== prevProps.auth.user._id) {
    this.props.getMyLessons(user._id);
  }
}
相关问题