简体   繁体   English

渲染和子道具之间的反应路由器差异

[英]React router difference between render and children prop

I wanted to understand the exact differences between the 2 ways of writing React <Route /> ie one with "render" and other with "children" prop;我想了解两种编写 React <Route />的方式之间的确切区别,即一种使用“render”,另一种使用“children” prop;

// With "render"
    <Route
          path={route.path}
          render={props => (
            // pass the sub-routes down to keep nesting
            <route.component {...props} routes={route.routes} />
          )}
        />

// With "children"
    <Route
            path={route.path}
            exact={route.exact}
            children={props => <route.component {...props} routes={route.routes} />}
          />

Also, with the 2nd use case (ie with "children"), what difference does it make when exact property is true v/s false?此外,对于第二个用例(即“孩子”),当确切属性为真与假时有什么区别?

From the docs of从文档

React Router's render function

This allows for convenient inline rendering and wrapping without the undesired remounting explained above.Instead of having a new React element created for you using the component prop, you can pass in a function to be called when the location matches这允许方便的内联渲染和包装,而无需上面解释的不希望的重新安装。您可以传入 function 以在location matches时调用,而不是使用组件属性为您创建新的 React 元素

And

React Router's children function

Sometimes you need to render whether the path matches the location or not .有时您需要渲染path matches the location or not In these cases, you can use the function children prop.在这些情况下,您可以使用 function 儿童道具。 It works exactly like render except that it gets called whether there is a match or not .它的工作原理与 render 完全一样,只是无论是否match or not都会被调用。

The main difference as per what I understand is that when we pass a component as children prop then that component will get rendered even if the location matches or not .据我了解,主要区别在于,当我们将组件作为children属性传递时,即使位置matches or not ,该组件也会被渲染。

I have created a sample app in order to simulate the same, here is the app我创建了一个示例应用程序来模拟相同的, 这里是应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM