![](/img/trans.png)
[英]Redux - are there any drawback to using bindActionCreators in the constructor of a React component?
[英]Using bindActionCreators and mapDispatchToProps in a React functional component gives me undefined
在功能組件中,我將bindActionCreators
與mapDispatchToProps
一起使用。 當我嘗試通過props
訪問它時,我什么也沒看到。 下面是我如何使用它們。
function mapDispatchToProps(dispatch) {
const actions = Object.assign({}, saveActions);
return { actions: bindActionCreators(actions, dispatch) }
}
export default connect(null, mapDispatchToProps)(App);
在我的return
中,
我有
return (
...
<SaveModal actions={props.actions} />
...
)
當我在SaveModal
中檢查this.props.actions
時,它是一個 Class 組件,它沒有定義。 當我在App
中檢查props.actions
時,它也是未定義的。 我錯過了什么嗎?
取決於safeActions
的形狀。
你這樣做的方式,每個safeActions.x
都變成了prop.x
。
另外,請注意,您可以這樣做
export default connect(null, safeActions)(App);
以達到完全相同的結果。
然后請注意,對於 function 組件,我們建議使用useDispatch
和useSelector
掛鈎而不是使用connect
,因為它更容易處理。
https://redux.js.org/style-guide/style-guide/#use-the-react-redux-hooks-api
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.