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

我的应用程序遇到问题,我想根据已登录的用户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);
yymbaya 回答:我应该在生命周期中的什么地方响应Redux

您可以在componentDidUpdate中进行操作:

componentDidUpdate(prevProps) {
  const {user} = this.props.auth;
  if (user && user._id && user._id !== prevProps.auth.user._id) {
    this.props.getMyLessons(user._id);
  }
}
本文链接:https://www.f2er.com/3168676.html

大家都在问