[英]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>
'smatch
via thewithRouter
higher-order component.您可以通过
withRouter
高阶组件访问history
对象的属性和最接近的<Route>
的match
。withRouter
will pass updatedmatch
,location
, andhistory
props to the wrapped component whenever it renders.withRouter
会在withRouter
将更新的match
,location
和history
道具传递给包装组件。
import { withRouter } from 'react-router-dom';
class News extends React.Component {
...
}
export default withRouter(News);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.