redux state 显示数据,但是 props returns null inside the component function
redux state shows data, but props returns null inside the function of the Component
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 数据最初显示为空,在代码中发布的主组件函数内部调用调度操作之后,这里重要的是,在调用调度操作时获取 api被触发,数据存储到 redux 存储中,状态也被更新。问题是我们可以访问主组件内的 redux 存储数据,但不能访问主组件内的函数。如果我在主要组件中记录 props 数据,它会显示更新的状态数据,但如果在函数中记录相同的内容,那么最初它会在加载(日志)数据后显示 null。这让我很困扰。关于这一点,我尝试使用 store.subcribe(listener) where function get data when store is updated inside main component.它有效,但会花费太多时间。所以这不适合我的情况。所以非常感谢任何帮助。
谢谢。
您必须注意到存储是异步更新的,因此即使在您使用 await 语法之后,您也不确定最新的值是否已经存在以记录它们。
您正在 handle_edit_click 函数之外访问日志语句中的最新值,原因很简单,因为您的 redux 存储会通知您的组件新道具可用,与您登录时尚不可用的新道具相同在你的 handle_edit_click
里面
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 数据最初显示为空,在代码中发布的主组件函数内部调用调度操作之后,这里重要的是,在调用调度操作时获取 api被触发,数据存储到 redux 存储中,状态也被更新。问题是我们可以访问主组件内的 redux 存储数据,但不能访问主组件内的函数。如果我在主要组件中记录 props 数据,它会显示更新的状态数据,但如果在函数中记录相同的内容,那么最初它会在加载(日志)数据后显示 null。这让我很困扰。关于这一点,我尝试使用 store.subcribe(listener) where function get data when store is updated inside main component.它有效,但会花费太多时间。所以这不适合我的情况。所以非常感谢任何帮助。 谢谢。
您必须注意到存储是异步更新的,因此即使在您使用 await 语法之后,您也不确定最新的值是否已经存在以记录它们。 您正在 handle_edit_click 函数之外访问日志语句中的最新值,原因很简单,因为您的 redux 存储会通知您的组件新道具可用,与您登录时尚不可用的新道具相同在你的 handle_edit_click
里面