首页 > 解决方案 > 如何将道具传递给组件(使用钩子),该组件用于从渲染道具编程中获取道具

问题描述

我收到了一个使用渲染道具和路线的程序:

<MyRoute exact module="basic" action="show" resource="Document" path="..." component={MyComponent} />

然后 MyRoute 会:

const MyRoute = ({ ...props }) => {
    const { component: MyComponent, ...otherProps } = props;

    return (
        <Route
            {...otherProps}
            render={rest =>
               <MyComponent {...rest} />
            }
        />
    );
};

export default MyRoute;

otherProps 和 rest 的 console.log 是:

Rest: 
history: {length: 1, action: "POP", location: {…}, createHref: ƒ, push: ƒ, …}
location: {pathname: "/document_preview/335", search: "?employee=82", hash: "", state: undefined}
match: {path: "/document_preview/:id", url: "/document_preview/335", isExact: true, params: {…}}
staticContext: undefined
[[Prototype]]: Object
------------------------------------------
otherProps:
action: "show"
computedMatch: {path: "/document_preview/:id", url: "/document_preview/335", isExact: true, params: {…}}
exact: true
location: {pathname: "/document_preview/335", search: "?employee=82", hash: "", state: undefined}
module: "basic"
path: "/document_preview/:id"
resource: "Document"
[[Prototype]]: Object

我需要能够在模态中安装整个组件,我需要能够执行和交付它需要的所有道具,而不使用渲染道具和路由,只使用钩子

你认为我在这里的选择是什么?

拉斐尔

标签: reactjsreact-routerreact-hooksmodal-dialog

解决方案


推荐阅读