当前位置: 首页 > 面试题库 >

在Link react-router中传递道具

冉永宁
2023-03-14
问题内容

我正在用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;

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


问题答案:

此行缺失path

<Route name="ideas" handler={CreateIdeaView} />

应该:

<Route name="ideas" path="/:testvalue" handler={CreateIdeaView} />

鉴于以下情况Link (过时的v1)

<Link to="ideas" params={{ testvalue: "hello" }}>Create Idea</Link>

自v4起最新

const backUrl = '/some/other/value'
// this.props.testvalue === "hello"
<Link to={{pathname: `/${this.props.testvalue}`, query: {backUrl}}} />

并在withRouter(CreateIdeaView)组件中render()

console.log(this.props.match.params.testvalue, this.props.location.query.backurl)
// output
hello /some/other/value

从您在文档上发布的链接,朝页面底部:

给定一条路线 <Route name="user" path="/users/:userId"/>

使用一些存根查询示例更新了代码示例:

// import React, {Component, Props, ReactDOM} from 'react';

// import {Route, Switch} from 'react-router'; etc etc

// this snippet has it all attached to window since its in browser

const {

  BrowserRouter,

  Switch,

  Route,

  Link,

  NavLink

} = ReactRouterDOM;



class World extends React.Component {

  constructor(props) {

    super(props);

    console.dir(props);

    this.state = {

      fromIdeas: props.match.params.WORLD || 'unknown'

    }

  }

  render() {

    const { match, location} = this.props;

    return (

      <React.Fragment>

        <h2>{this.state.fromIdeas}</h2>

        <span>thing:

          {location.query

            && location.query.thing}

        </span><br/>

        <span>another1:

        {location.query

          && location.query.another1

          || 'none for 2 or 3'}

        </span>

      </React.Fragment>

    );

  }

}



class Ideas extends React.Component {

  constructor(props) {

    super(props);

    console.dir(props);

    this.state = {

      fromAppItem: props.location.item,

      fromAppId: props.location.id,

      nextPage: 'world1',

      showWorld2: false

    }

  }

  render() {

    return (

      <React.Fragment>

          <li>item: {this.state.fromAppItem.okay}</li>

          <li>id: {this.state.fromAppId}</li>

          <li>

            <Link

              to={{

                pathname: `/hello/${this.state.nextPage}`,

                query:{thing: 'asdf', another1: 'stuff'}

              }}>

              Home 1

            </Link>

          </li>

          <li>

            <button

              onClick={() => this.setState({

              nextPage: 'world2',

              showWorld2: true})}>

              switch  2

            </button>

          </li>

          {this.state.showWorld2

           &&

           <li>

              <Link

                to={{

                  pathname: `/hello/${this.state.nextPage}`,

                  query:{thing: 'fdsa'}}} >

                Home 2

              </Link>

            </li>

          }

        <NavLink to="/hello">Home 3</NavLink>

      </React.Fragment>

    );

  }

}





class App extends React.Component {

  render() {

    return (

      <React.Fragment>

        <Link to={{

          pathname:'/ideas/:id',

          id: 222,

          item: {

              okay: 123

          }}}>Ideas</Link>

        <Switch>

          <Route exact path='/ideas/:id/' component={Ideas}/>

          <Route path='/hello/:WORLD?/:thing?' component={World}/>

        </Switch>

      </React.Fragment>

    );

  }

}



ReactDOM.render((

  <BrowserRouter>

    <App />

  </BrowserRouter>

), document.getElementById('ideas'));


<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/4.3.1/react-router-dom.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/4.3.1/react-router.min.js"></script>



<div id="ideas"></div>

参见:https :
//github.com/ReactTraining/react-
router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-
guides/v2.0.0.md#link-to-onenter-and-isactive-use-location-
descriptors


从1.x到2.x的升级指南:

<Link to>,onEnter和isActive使用位置描述符

<Link to>现在可以使用除字符串之外的位置描述符。查询和状态道具已弃用。

// v1.0.x

<Link to="/foo" query={{ the: 'query' }}/>

// v2.0.0

<Link to={{ pathname: '/foo', query: { the: 'query' } }}/>

//在2.x版本中仍然有效

<Link to="/foo"/>

同样,从onEnter挂钩重定向现在也使用了位置描述符。

// v1.0.x

(nextState, replaceState) => replaceState(null, '/foo')
(nextState, replaceState) => replaceState(null, '/foo', { the: 'query'

})

// v2.0.0

(nextState, replace) => replace('/foo')
(nextState, replace) => replace({ pathname: '/foo', query: { the:

‘query’ } })

对于自定义的类似链接的组件,同样适用于router.isActive,以前是history.isActive。

// v1.0.x

history.isActive(pathname, query, indexOnly)

// v2.0.0

router.isActive({ pathname, query }, indexOnly)

从v3到v4的更新:

  • https://github.com/ReactTraining/react-router/blob/432dc9cf2344c772ab9f6379998aa7d74c1d43de/packages/react-router/docs/guides/migrating.md

  • https://github.com/ReactTraining/react-router/pull/3803

  • https://github.com/ReactTraining/react-router/pull/3669

  • https://github.com/ReactTraining/react-router/pull/3430
  • https://github.com/ReactTraining/react-router/pull/3443
  • https://github.com/ReactTraining/react-router/pull/3803
  • https://github.com/ReactTraining/react-router/pull/3636
  • https://github.com/ReactTraining/react-router/pull/3397
  • https://github.com/ReactTraining/react-router/pull/3288

该接口基本上与v2相同,最好查看react-router的CHANGES.md,因为这是更新所在。

后代的“旧版迁移文档”

  • https://github.com/ReactTraining/react-router/blob/dc7facf205f9ee43cebea9fab710dce036d04f04/packages/react-router/docs/guides/migrating.md
  • https://github.com/ReactTraining/react-router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-guides/v1.0.0.md
  • https://github.com/ReactTraining/react-router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-guides/v2.0.0.md
  • https://github.com/ReactTraining/react-router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-guides/v2.2.0.md
  • https://github.com/ReactTraining/react-router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-guides/v2.4.0.md
  • https://github.com/ReactTraining/react-router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-guides/v2.5.0.md


 类似资料:
  • 我正在浏览React Router网站上给出的传递我们自己的道具的示例: https://reacttraining.com/react-router/core/api/Route/render-func 问:{…rest}是为了将…rest对象传递给routeProps而被传递给的吗? 塔克斯。A.

  • 问题内容: 我无法克服React Router的问题。场景是我需要让子级路由从状态父级组件和路由传递一组道具。 我想做的就是通过它,然后通过它。但是,我能弄清楚如何做到这一点的唯一方法是同时通过两者,这意味着每条子路线都会获得每条子道具,而不论其是否相关。目前,这并不是一个阻塞问题,但是我可以看到有一段时间我将使用两个相同的组件,这意味着propA上的键将被propB的键覆盖。 这实际上以我期望的

  • 问题内容: 我需要使用路由器将道具传递到组件。这是我的代码: 如您所见,isAuthenticated我想传递给Login组件的道具。 当我登录道具时, isAuthenticated 道具不存在。我做错了什么?如何正确通过道具?我关注了文档以及其他讨论。据我了解,它应该可以工作。的版本 做出反应路由器 和 反应路由器- DOM 是 4.0.0 问题答案: 像这样传递它: 它应该在“登录组件”中可

  • 问题内容: 我正在尝试将现有的react-router 3.0.0应用程序迁移到4.0.0。在路由器内,我需要传递与其子代无关的其他非路由选择。 在早期版本的react- router中,有一些技巧性的方法可以实现此目的,本文对此进行了详细介绍。我个人最喜欢的是使用的这种方法,因为它可以确保复制所有的react-router道具: 到目前为止,我的新App组件如下所示: 在保留提供的路由器道具的同

  • 问题内容: 我使用React Router的React.js应用程序具有以下结构: 我想将一些属性传递给组件。 (通常我会这样做) 用React Router做到这一点最简单,正确的方法是什么? 问题答案: 更新 自新版本发布以来,无需使用包装器就可以直接通过组件传递道具。例如,通过使用prop。 零件: 用法: 旧版本 我的首选方式是包装组件并将包装器作为路由处理程序传递。 这是您应用了更改的示

  • 嗨,我想从一个页面移动到另一个页面,并传递paramters和。如果URL中没有这些参数,我可以用react router实现这一点吗? 我正在使用查看https://github.com/rackt/react-router/blob/master/docs/guides/overview.md#dynamic-segments和解决方案,但在将params传递给URL之前,它无法工作。 编辑1