简体   繁体   English

如何在React路由器中传递道具

[英]How to pass props in react router

I have an App, a Navbar and a Content class, I am trying to pass a prop from navbar to content that will be rendered when I redirect to the content page. 我有一个应用程序,一个Navbar和一个Content类,我正在尝试将一个道具从navbar传递到当我重定向到内容页面时将呈现的内容。

App.js App.js

import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import Navbar from './components/Home/Navbar';
import Content from './components/Home/Content';

export default class App extends Component {

  render() {
      return (
        <Router>
            <div>
              <Navbar />
              <Route path="/content" component={Content} render={(props) => <Navbar {...props} test={this.state.test} />} />
            </div>
        </Router>
    );
  }
}

Navbar.js Navbar.js

import React from 'react';

class Navbar extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            test: 'test',
        }
    }

    render() {
        return (

            <div>
                This is my navbar
            </div>

        );
    }
}

export default Navbar;

Content.js Content.js

import React from 'react';

class Content extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            x: 'x',
            test: this.props.test
        }
    }

    render() {
        return (

            <div>
                <p>{this.state.x}</p>
                <p>{this.state.test}</p>
            </div>

        );
    }
}

export default Content;

The problem that I am having is that is when I redirect to the content page, the state from the navbar class is not being passed through to it. 我遇到的问题是,当我重定向到内容页面时,导航栏类的状态没有传递给它。 How do I fix this? 我该如何解决?

The problem mostly lays in the fact that you are using both component and render props in your Route , you should use only one. 问题主要在于您在Route同时使用了componentrender道具,因此您只能使用其中一个。 If you do not want to change or pass along anything from where your Route is defined, you should use component property. 如果你不想改变或沿着从任何地方通过Route定义,你应该使用component属性。

If you do wish to pass along some information at that point, use the render prop as you have done (however, I believe you really wanted to render the Content component and not the NavBar as in your OP) 如果您确实希望在那时传递一些信息,请使用已完成的render道具(但是,我相信您确实想渲染Content组件,而不是OP中的NavBar )。

<Route path="/content" render={(props) => <Content {...props} test={this.state.test} />} />

Then you really don't need any of your local state you were displaying, and content could be a functional component instead, like 然后,您实际上不需要显示的任何本地状态,而是内容可以是功能组件,例如

const Content = ({ x, test }) => (
  <>
    <p>{ x }</p>
    <p>{ test }</p>
  </>);

where x and test would be destructured from your props, giving you easy access to it (you could also use (props) and then props.test and props.x depending on how you like to write it) xtest将从props中解构出来,使您可以轻松访问它(也可以使用(props) ,然后使用props.testprops.x具体取决于您的编写方式)

u can pass state like this with redirect : 你可以通过重定向传递这样的状态:

<Redirect to={{
            pathname: '/content',
            state: { test: '123' }
        }}
/>

and for accessing it : 和访问它:

this.props.location.state.test

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

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