简体   繁体   English

反应链接发送道具

[英]react link send props

I'm new to react, so please be kind and bear with me :D 我是新来的反应者,所以请客气并忍受我:D

Here's my problem: 这是我的问题:

I have a lsit of projectItem components that are being generated and all are linked to a projectDetail page about that project, something like this: 我对正在生成的projectItem组件不满意,所有组件都链接到有关该项目的projectDetail页面,如下所示:

render() {
    return (
      <Link to="/projectDetail" >
        <div className="projectItem">
          <h1>{this.props.name}</h1>
          <LazyLoad height={400} offsetTop={0} imageProps={{
            src: this.props.thumb,
            alt: this.props.name,
            ref: "image",
          }} />
         </div>
        </Link>
    );
  }

and my Route which is located in app.js is something like this: 而位于app.js中的我的路线是这样的:

<Route path="/projectDetail" component={ProjectDetail} />

Now, I would like to send some properties to projectDetail page as well, so that I could tell which projectItem is clicked, How should I? 现在,我也想将一些属性发送到projectDetail页面,这样我才能知道单击了哪个projectItem,应该怎么做?

You cant do this by passing props, or at least its like the worst practice ever! 您不能通过传递道具来做到这一点,或者至少像有史以来最糟糕的做法那样! You want to pass some pk (primary key) or rk or slug to the detail view. 您想要将pk(主键)或rk或slug传递到详细信息视图。 Consider making a urls variable like this: 考虑制作一个这样的urls变量:

const urls = {
  frontend: {
    project: {
      list: () => '/projects/',
      details: (pk) => `/projects/detail/${pk ? pk : ':pk:'}`,
    }
}

and in your route, do something like this: 在您的路线上,执行以下操作:

<Route path={urls.frontend.details()} exact component={ProjectDetail} />

and when you want to use this rout in a link, do this: 当您想在链接中使用该溃败时,请执行以下操作:

<Link to={urls.frontend.details(<your_pk_here>)} >
  <YourContent>
</Link>

You can do this by simple way here: 您可以在此处通过简单的方式执行此操作:

    <Link to="projectDetail" params={{ testvalue: "hello" }}>Your link</Link>

or 要么

    <Link to="projectDetail/hello">Your link</Link>

And, 和,

    <Route name="projectDetail" path="/:testvalue" component={ProjectDetail}/>

to get this via this.props.params.value at your CreateIdeaView class. 可以通过CreateIdeaView类中的this.props.params.value获取此信息。

You can pass projectID or whatever using following code 您可以使用以下代码传递projectID或其他任何内容

render() {
  return (
    <Link to={{
      pathname: '/projectDetail',
      state: { projectID: ID }
    }}>
      <div className="projectItem">
        <h1>{this.props.name}</h1>
        <LazyLoad height={400} offsetTop={0} imageProps={{
          src: this.props.thumb,
          alt: this.props.name,
          ref: "image",
        }} />
        </div>
      </Link>
  );
}

You can then use location.state.projectID to access it. 然后,您可以使用location.state.projectID进行访问。

You can use the Link component like below, you may use this as reference https://reacttraining.com/react-router/web/api/Link 您可以使用如下所示的链接组件,也可以将其用作参考https://reacttraining.com/react-router/web/api/Link

render() {
    return (
      <Link to={{pathname: '/projectDetail',state: { //state to persists} }}>
        <div className="projectItem">
          <h1>{this.props.name}</h1>
          <LazyLoad height={400} offsetTop={0} imageProps={{
            src: this.props.thumb,
            alt: this.props.name,
            ref: "image",
          }} />
         </div>
        </Link>
    );
  }

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

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