如何在连接的Redux选择器中使用React Route Params?

问题描述 投票:0回答:1

我有一个连接到Redux的组件:

interface EditProgramCategoryProps {
    category: ProgramCategory;
    fetchAllCategories: () => void;
}

const PureEditProgramCategory: React.FC<EditProgramCategoryProps> = ({
                                                                         category , fetchAllCategories,
                                                                     }) => {
    let { id } = useParams();

    useEffect(() => {
        fetchAllCategories();
    }, [fetchAllCategories]);

    return (...);
};

const mapStateToProps => createSelector(
    [ProgramCategorySelectors.getById(id)], <-- this one needs an id
    (category) => ({ category }),
);

const mapDispatchToProps = {
    fetchAllCategories: ProgramCategoryActions.fetchAll.start,
};

export const EditProgramCategory = connect(mapStateToProps, mapDispatchToProps)(PureEditProgramCategory);

ProgramCategorySelectors.getById需要一个来自路由的ID。使选择器可用的正确方法是什么?我看到的一种方法是先将ID推入redux,但这并不正确。

reactjs redux react-redux react-router
1个回答
-1
投票

您可以使用this.props.location.search从url获取查询字符串值

© www.soinside.com 2019 - 2024. All rights reserved.