[英]Why the redux state is undefined after my action call inside componentDidMount?
I have a problem annoying me for a while so here it is... I'm trying to fetch details from an API of mine, so I created an async action creator and as it is usually done I invoke it inside the componentDidmount()
.我有一个问题让我烦恼了一段时间,所以这里是......我正在尝试从我的 API 中获取详细信息,所以我创建了一个异步操作创建器,并且通常会在
componentDidmount()
调用它. the problem is I want to access the props immediately after the action call but whenever I run console.log(this.props.note)
it prints undefined.问题是我想在操作调用后立即访问道具,但是每当我运行
console.log(this.props.note)
它都会打印 undefined。 so how can I access the redux state after the call?那么如何在调用后访问 redux 状态呢?
My action:我的行动:
export const fetchNoteDetail = (id, ) => async dispatch => {
const response = await notes.get(`notes/${id}`)
dispatch({type: 'FETCH_NOTE_DETAIL', payload: response.data})
}
ComponentDidMount(): ComponentDidMount():
componentDidMount() {
const {id} = this.props.match.params
this.props.fetchNoteDetail(id)
console.log(this.props.note) //undefined
}
在componentDidUpdate()
运行这个console.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.