我的 mapStateToProps 需要組件 urlParams 來選擇 redux 存儲的一部分,我嘗試使用但我一直收到錯誤:“TypeError: ownProps.useParams is not a function”我的代碼
如果我正確理解您的問題/代碼,您正在嘗試使用collectionId
路由匹配參數作為selectCollection
選擇器的鍵。
在react-router-dom
V6有這麼渲染組件不再路線道具需要使用掛鉤做出反應訪問navigate
,location
和match
/ params
。您可以走創建自定義withRouter
HOC的路線,將匹配參數作為道具注入,並可供connect
HOC訪問,但完全沒有必要使用兩個高階組件。
我建議在組件中使用鉤子。
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
const CollectionPage = () => {
const { collectionId } = useParams();
const collection = useSelector(selectCollection(collectionId));
return (
... JSX ...
);
};
export default CollectionPage;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句