如果没有 componentWillReceiveProps,react-thunk func 应该去哪里?
Where should react-thunk func go if no componentWillReceiveProps?
自从 React 16.3 到来后,我正在尝试重构我的代码以使用新的和首选的 static getDerivedStateFromProps()
,但这就是我目前在我的 componentWillReceiveProps
中拥有的
componentWillReceiveProps(nextProps) {
if (nextProps.chatId !== this.props.chatId)
this.subscribeChatMessages(nextChatId) // a redux-thunk function
}
既然静态函数不能从this
调用任何东西,我该怎么办?
我可以把 redux-thunk 功能移到 shouldComponentUpdate
,但是好像不对?
由于 subscribeChatMessages(..)
是具有 side-effect 的 redux-thunk-action 函数,您应该将其放在 componentDidUpdate(prevProps, prevState, snapshot)
.
中
componentDidUpdate(prevProps) {
if (nextProps.chatId !== this.props.chatId)
this.subscribeChatMessages(nextChatId) // a redux-thunk function
}
自从 React 16.3 到来后,我正在尝试重构我的代码以使用新的和首选的 static getDerivedStateFromProps()
,但这就是我目前在我的 componentWillReceiveProps
componentWillReceiveProps(nextProps) {
if (nextProps.chatId !== this.props.chatId)
this.subscribeChatMessages(nextChatId) // a redux-thunk function
}
既然静态函数不能从this
调用任何东西,我该怎么办?
我可以把 redux-thunk 功能移到 shouldComponentUpdate
,但是好像不对?
由于 subscribeChatMessages(..)
是具有 side-effect 的 redux-thunk-action 函数,您应该将其放在 componentDidUpdate(prevProps, prevState, snapshot)
.
componentDidUpdate(prevProps) {
if (nextProps.chatId !== this.props.chatId)
this.subscribeChatMessages(nextChatId) // a redux-thunk function
}