我应该把我的 dispatch 放在生命周期 react redux 的什么地方

Where should I put my dispatch in lifecycle react redux

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

我试图在 componentDidMount 中发送我的 getMyLessons 但它需要 this.props.auth.user 作为 null 参数,但是如果我在 render() 上使用它它加载(它被冻结但加载它哈哈),所以我认为它是一个 lyfecicle 问题

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);

您可以在 componentDidUpdate:

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