在Link react-router中传递道具

JavaScript

凯西里

2020-03-11

我正在用react-router进行反应。我正在尝试在react-router的“链接”中传递属性

var React  = require('react');
var Router = require('react-router');
var CreateIdeaView = require('./components/createIdeaView.jsx');

var Link = Router.Link;
var Route = Router.Route;
var DefaultRoute = Router.DefaultRoute;
var RouteHandler = Router.RouteHandler;
var App = React.createClass({
  render : function(){
    return(
      <div>
        <Link to="ideas" params={{ testvalue: "hello" }}>Create Idea</Link>
        <RouteHandler/>
      </div>
    );
  }
});

var routes = (
  <Route name="app" path="/" handler={App}>
    <Route name="ideas" handler={CreateIdeaView} />
    <DefaultRoute handler={Home} />
  </Route>
);

Router.run(routes, function(Handler) {

  React.render(<Handler />, document.getElementById('main'))
});

“链接”呈现页面,但不将属性传递给新视图。下面是查看代码

var React = require('react');
var Router = require('react-router');

var CreateIdeaView = React.createClass({
  render : function(){
    console.log('props form link',this.props,this)//props not recived
  return(
      <div>
        <h1>Create Post: </h1>
        <input type='text' ref='newIdeaTitle' placeholder='title'></input>
        <input type='text' ref='newIdeaBody' placeholder='body'></input>
      </div>
    );
  }
});

module.exports = CreateIdeaView;

如何使用“链接”传递数据?

第791篇《在Link react-router中传递道具》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

5个回答
JimAGil 2020.03.11

请参阅此帖子以供参考

简单的是:

<Link to={{
     pathname: `your/location`,
     state: {send anything from here}
}}

现在您要访问它:

this.props.location.state
路易Itachi小小 2020.03.11

要解决以上答案(https://stackoverflow.com/a/44860918/2011818),您还可以在Link对象内将对象“ In”内联发送。

<Route path="/foo/:fooId" component={foo} / >

<Link to={{pathname:/foo/newb, sampleParam: "Hello", sampleParam2: "World!" }}> CLICK HERE </Link>

this.props.match.params.fooId //newb
this.props.location.sampleParam //"Hello"
this.props.location.sampleParam2 //"World!"
十三西里GO 2020.03.11

至于react-router-dom 4.xx(https://www.npmjs.com/package/react-router-dom),您可以将params传递给组件以通过以下路径进行路由:

<Route path="/ideas/:value" component ={CreateIdeaView} />

通过链接(考虑将testValue属性传递给呈现链接的相应组件(例如,上面的App组件))

<Link to={`/ideas/${ this.props.testValue }`}>Create Idea</Link>

将props传递给组件构造函数,则可以通过以下方式使用value参数

props.match.params.value
神乐蛋蛋 2020.03.11
 <Link
    to={{
      pathname: "/product-detail",
      productdetailProps: {
       productdetail: "I M passed From Props"
      }
   }}>
    Click To Pass Props
</Link>

路由重定向的另一端执行此操作

componentDidMount() {
            console.log("product props is", this.props.location.productdetailProps);
          }
LEY米亚 2020.03.11

有一种方法可以传递多个参数。您可以将“ to”作为对象而不是字符串。

// your route setup
<Route path="/category/:catId" component={Category} / >

// your link creation
const newTo = { 
  pathname: "/category/595212758daa6810cbba4104", 
  param1: "Par1" 
};
// link to the "location"
// see (https://reacttraining.com/react-router/web/api/location)
<Link to={newTo}> </Link>

// In your Category Component, you can access the data like this
this.props.match.params.catId // this is 595212758daa6810cbba4104 
this.props.location.param1 // this is Par1

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android