简体   繁体   English

如何在React中的两个不相关和无状态组件之间传递道具?

[英]How to pass props between two not-related and stateless component in React?

As title said, i'm try to find a way to pass my props from one component to anotehr taht are not related between each others. 正如标题所说,我试图找到一种方法将我的道具从一个组件传递到另一个组件,彼此之间没有关系。 The only thing that related them is a link: 唯一与之相关的是链接:

const ConversationPreview = (props) => {
  var people = props.people;
  const messages = props.messages;

  const conversationName = people.map(person => {

    // Filter message from conversation's sender
    function filterMessageBySender(obj){
      if ('from' in obj && obj.from === person.nickname) {
        return true;
      }
    }

    const messagesByCurrentSender = messages.filter(filterMessageBySender);
    const lastMsg = messagesByCurrentSender[messagesByCurrentSender.length - 1]

    function passProps() {
      return <Conversation people={people} />
    }

    return (
      <li key={person.id}>
        <Link to={`/${person.nickname}`} onClick={passProps}>
          <Avatar image={person.pic} />
          {person.nickname}
          <p> {lastMsg.body} </p>
        </Link>
      </li>
    )
  });

  return (
    <ul>
      {conversationName}
      {props.children}
    </ul>

  )
};

And this is the component that should receive the people props: 这是应该接收人们道具的组件:

const Conversation = (props) => {

  console.log(props);
  //console.log(people);

  function findConversationPerson(person){
    return person.nickname === props.params.conversation
  }
  const currentPerson = peopleOld.find(findConversationPerson); 


  return (
    <div>
      <header>
        <Avatar image={currentPerson.pic} />
        <h1> {props.params.conversation} </h1>
      </header>
      <main>
        <MessagesList sender={currentPerson}/>
      </main>
    </div>
  )
};

Is there a(good) way or should I re-think the whole way i structured the app? 是否有(好的)方式或者我应该重新思考我构建应用程序的整个方式?

After lot of resource i discover this is quite a common problem and is mostly related to react-router. 经过大量资源后,我发现这是一个非常常见的问题,主要与react-router有关。 The problem is that usually in react we pass props inside the component which have to be explicitly defined in the render func. 问题是通常在反应中我们在组件内部传递道具,这些道具必须在渲染函数中明确定义。 But sometimes we don't want to render them directly and maybe the router take care about when and where render them. 但有时我们不想直接渲染它们,也许路由器会关注渲染它们的时间和地点。 So how to pass props? 那么如何传递道具?

A way to do it is to clone the children with the passed data. 一种方法是使用传递的数据克隆子项。 So, in your App render function you will have: 因此,在您的App渲染功能中,您将拥有:

{React.cloneElement(this.props.children, {dataChildrenNeeded: this.props.parentsData})}

So all your children will have same props. 所以你的孩子都会拥有相同的道具。 And they can easily be access by doing: 他们可以通过以下方式轻松访问:

this.props.dataChildrenNeeded 

This will work with state or whateverlse you want to pass. 这将适用于您想传递的州或其他任何内容。

For better explanation and further reading here are my main sources: 为了更好的解释和进一步阅读,我的主要来源是:

Stackoverflow: React router and this.props.children Stackoverflow:React router和this.props.children

react-router github issue #1857 react-router github issue#1857

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

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