小编her*_*ose的帖子

如何将 URL 参数传递给选择器

我收到一个来自 的 url 参数useParams。我想使用将它传递给选择器mapStateToProps

集合.组件.jsx

import { useParams } from "react-router-dom";
import { connect } from "react-redux";

import { selectShopCollection } from "../../redux/shop/shop.selectors";

import './collection.styles.scss'

const Collection = ({ collection }) => {
  const { collectionId } = useParams();
  console.log(collection)
  return (
    <div>
      <h1>{collection}</h1>
    </div>
  )
}

const mapStateToProps = (state, ownProps) => ({
  collection: selectShopCollection(ownProps.match.params.collectionId)(state)
})

export default connect(mapStateToProps)(Collection);
Run Code Online (Sandbox Code Playgroud)

shop.selectors.js

import { createSelector } from "reselect"

const selectShop = state => state.shop

export const selectShopCollections …
Run Code Online (Sandbox Code Playgroud)

reactjs reselect react-router-dom react-hooks mapstatetoprops

2
推荐指数
1
解决办法
1017
查看次数