简体   繁体   English

React react-router-dom将props传递给组件

[英]React react-router-dom pass props to component

I need to pass props to component using router. 我需要使用路由器将props传递给组件。 Here's my code: 这是我的代码:

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import AppBarTop from './appbar/AppBarTop';

import Login from '../pages/login/Login';
import {BrowserRouter as Router, Route} from 'react-router-dom';


class App extends Component {

    render() {

        const { isAuthenticated } = this.props;

        return (
            <Router>
                <div>
                    <AppBarTop isAuthenticated={isAuthenticated} />
                    <div className="content">
                        <Route path="/login" isAuthenticated={isAuthenticated} component={Login} />
                    </div>
                </div>
            </Router>
        );
    }
}

As you can see, isAuthenticated the prop i want to pass to Login component. 如您所见,isAuthenticated我要传递给Login组件的prop。

class Login extends Component {

    constructor(props) {
        super(props);
        console.log(props);
    }

    render() {
        return (
            <LoginForm />
        );
    }

}

export default connect(null) (Login);

When i log the props the isAuthenticated prop is not there. 当我记录道具时, isAuthenticated道具不存在。 What i'm doing wrong? 我做错了什么? How can i pass the prop correctly? 我怎样才能正确通过道具? I followed the docs and also other discussions. 我跟着文档和其他讨论。 From my understanding it should work. 从我的理解,它应该工作。 The version of react-router and react-router-dom is 4.0.0 react-routerreact-router-dom的版本4.0.0

Pass it like this: 像这样传递:

<Route 
    path="/login" 
    render={(props) => <Login {...props} isAuthenticated={isAuthenticated}/>} 
/>

It should be available by this.props.isAuthenticated in Login Component. 它应该由this.props.isAuthenticated在Login Component中提供。

Reason of {...props} : {...props}原因:

If we don't write this then only isAuthenticated will get passed to Login component, all other values that router passes to component, will not be available inside Login component. 如果我们不写这个,那么只有isAuthenticated将被传递给Login组件,路由器传递给组件的所有其他值将不会在Login组件中可用。 When we write {...props} then we are passing all the values with one extra value. 当我们写{...props}我们将所有值传递给一个额外的值。

And instead of using component with router use render method. 而不是使用带路由器的component使用render方法。

As per DOC : 根据DOC

Component : 组件

When you use component (instead of render or children, below) the router uses React.createElement to create a new React element from the given component. 当您使用组件(而不是下面的render或children)时,路由器使用React.createElement从给定组件创建新的React元素。 That means if you provide an inline function to the component attribute, you would create a new component every render. 这意味着如果为组件属性提供内联函数,则每次渲染都会创建一个新组件。 This results in the existing component unmounting and the new component mounting instead of just updating the existing component. 这导致现有组件卸载和新组件安装,而不是仅更新现有组件。 When using an inline function for inline rendering, use the render. 使用内联函数进行内联渲染时,请使用渲染。

Render : 渲染

This allows for convenient inline rendering and wrapping without the undesired remounting. 这允许方便的内联呈现和包装而没有不期望的重新安装。

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

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