0
const DoctorResearchArticle = props => {

console.log('props.doctor_indi_articles', props.doctor_articles); //here can able to access redux store data after triggerring the dispatch action at line no 6

const handle_edit_click = async (id) => {
await props.doctor_ind_article(id);
console.log('props.doctor_indi_articles', props.doctor_articles);
// here after triggerring the api and dispatching the data to store, at the initial time it shows null
}

}
const mapDispatchToProps = (dispatch) => {
  return {
   doctor_ind_article: (article_id) =>dispatch(getDocIndivisualArti(article_id)),

  }
  }

  const mapStateToProps = (state) => {
  return {
    doctor_articles: state.doctor.doctor_ind_article
  };
};


export default connect(
  mapStateToProps,
  mapDispatchToProps
)(DoctorResearchArticle);

这里的问题是redux数据最初显示为null,在代码中发布的主要组件的函数内调用调度动作后,重要的是,在调用调度动作时触发get api并将数据存储到redux store状态也更新了。问题是我们可以访问主组件内的 redux 存储数据,但不能访问主组件内的函数。如果我在主组件中记录道具数据,它会显示更新的状态数据,但如果在函数内记录相同的内容,那么最初它会显示为 null,然后它会加载(记录)数据。这让我很困扰。关于这一点,我尝试使用 store.subcribe(listener) 函数在主组件内部更新存储时获取数据。它有效,但需要太多时间。所以这不适合我的情况。所以非常感谢任何帮助。谢谢。

4

1 回答 1

0

您必须注意到存储是异步更新的,因此即使在使用 await 语法之后,您也不确定是否已经有最新的值来记录它们。你在你的handle_edit_click函数之外访问你的日志语句中的最新值,原因很简单,你的redux商店通知你的组件有新的道具可用,当你在你的handle_edit_click中登录时,同样的新道具还不可用

于 2020-05-05T16:28:34.803 回答