简体   繁体   English

使用navLInk路由组件做出反应

[英]Route component using navLInk react

I'm trying to route a link and pass data. 我正在尝试路由链接并传递数据。 the problem is when I receive it on news component, all the things i pass it to return an undefined.. 问题是当我在新闻组件上收到它时,我传递给它的所有东西都返回一个未定义的..

app.js app.js

<BrowserRouter>
    <Route path="/news" component={News} />
</BrowserRouter>

parent.js parent.js

          <NavLink to={{
            pathname: '/news',
            state : { all : this.props.content} // this is what I want to send and I receive it from another
          }}>Todo</NavLink>

news.js news.js

export default class News extends React.Component {
constructor(props){
    super(props)
}

render(){
    const foo = this.props.location.state
    console.log(foo) // Cannot read property 'state' of undefined..
    console.log(this.props.location) // return undefined
    console.log(this.props) // return empty {}

    return (

        <div className='container'>
                   <section>
                       <h1>hello world </h1>
                   </section>
       </div>

   )
}

}

I am not sure where parent.js lives in your site structure. 我不确定parent.js在您的网站结构中的位置。 But I've done a codesandbox example for you. 但是我已经为你做了一个代码盒示例

I have defined a defaultProp in parent.js so some data is passed to the NavLink but you should be able to restructure the example for your needs. 我在parent.js定义了一个defaultProp,因此有些数据会传递给NavLink但您应该可以根据需要重新构建示例。

https://codesandbox.io/s/jp4qr3q3ww?autoresize=1&expanddevtools=1&fontsize=14 https://codesandbox.io/s/jp4qr3q3ww?autoresize=1&expanddevtools=1&fontsize=14

app.js (index.js) app.js(index.js)

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route } from "react-router-dom";

import News from "./news.js";
import Parent from "./parent.js";

import "./styles.css";

function App() {
  return (
    <BrowserRouter>
      <Route path="/" component={Parent} />
      <Route path="/news" component={News} />
    </BrowserRouter>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

parent.js parent.js

import React from "react";
import { NavLink } from "react-router-dom";

const Parent = props => (
  <NavLink
    to={{
      pathname: "/news",
      state: { all: props.content } // this is what I want to send and I receive it from another
    }}
  >
    Todo
  </NavLink>
);

Parent.defaultProps = {
  content: "some content"
};

export default Parent;

news.js news.js

import React from "react";

export default class News extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const foo = this.props.location.state;
    console.log(foo); // Cannot read property 'state' of undefined..
    console.log(this.props.location); // return undefined
    console.log(this.props); // return empty {}

    return (
      <div className="container">
        <section>
          <h1>hello world </h1>
        </section>
      </div>
    );
  }
}

Depending on how your app is structured your News component may not be receiving router props. 根据您的应用程序的结构,您的News组件可能无法接收路由器道具。 Try declaring your Route as follows: 尝试按如下方式声明您的Route

<Route
    path="/news"
    render={routerProps => <News {...routerProps}/>}
/>

Alternatively, you may want to try using withRouter : 或者,您可能想尝试使用withRouter

You can get access to the history object's properties and the closest <Route> 's match via the withRouter higher-order component. 您可以通过withRouter高阶组件访问history对象的属性和最接近的<Route>match withRouter will pass updated match , location , and history props to the wrapped component whenever it renders. withRouter会在withRouter将更新的matchlocationhistory道具传递给包装组件。

import { withRouter } from 'react-router-dom';

class News extends React.Component {
    ...
}

export default withRouter(News);

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

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