In my application, I want to pass the parameter that I'm getting from useParams() to the mapStateToProps function which I'm using with the Reselect library.
I can hard code the value instead of passing this parameter and everything works as expected. Also, I can pass useParams().routeName directly to the mapState function and it's working. But in case of using ownProps, it is not working. Here is my code:
const CollectionPage = ({collection}) => {
let params = useParams();
let collectionPath = params.routeName;
return (
<div className='collection-page'>
</div>
)
}
const mapStateToProps = (state, ownProps) => ({
collection: selectCollection(ownProps.collectionPath)(state)
});
export default connect(mapStateToProps)(CollectionPage);
With this code, the return will be undefined, but when I hard code the value, like the code below, it’s working:
const mapStateToProps = (state) => ({
collection: selectCollection(''Some Hard Coded Value'')(state)
});
Preferred
The preferred method would be to use the React hooks directly in the component. Instead of using the
connectHigher-Order Component (HOC), use theuseSelectorhook to select/access thecollectionstate. Refactor theselectCollectionselector to return the state and do the filtering in the UI.Example:
Alternative/Legacy
If you have the need to access path parameters in any
mapStateToPropsfunction, and if you are using a lot of other code, for example, then you'll need to create another HOC to access the path parameters and have them injected as props so they are available in themapStateToPropsfunction.Example:
...