简体   繁体   English

如何渲染几个相同的反应组件之一?

[英]How do I render one of several of the same react component?

I dont know if my brain is extra mushy today or what.我不知道我今天的大脑是不是特别糊状还是什么。 Im trying to learn react and now I have a list of messages that is rendered from an array.我试图学习反应,现在我有一个从数组呈现的消息列表。 Inside every message I have a button to render the "send message" component to answer the message.在每条消息中,我都有一个按钮来呈现“发送消息”组件来回答消息。 My problem is that when I press the button "answer", All the message-items in the list renders the 'send message' component.我的问题是,当我按下“回答”按钮时,列表中的所有消息项都会呈现“发送消息”组件。 How do I bind it to just one message I clicked answer in?如何将它绑定到我点击回答的一条消息? See my code and you will understand my question:看看我的代码,你就会明白我的问题:

render() {
let messages = this.state.messages.map((item, key) =>
  <div>
    <ul key={item.id} className="messageList">
      <li><h3 className="messageTitle">{item.title}</h3></li>
      <li>{item.message}</li>
      <li className="messageSignature">from: {item.from}</li>
    </ul>
    {this.state.showSendMessage ? <SendMessage recipient={item.from} title={item.title}></SendMessage> : null}
    <button onClick={this.toggleShowSendMsg.bind(this)}>Answer</button>
  </div>
);

Its because your state variable "showSendMessage" is not unique to any particular message and hence its becoming true for all the elements of messages.这是因为您的 state 变量“showSendMessage”对于任何特定消息都不是唯一的,因此对于消息的所有元素都适用。

Add one more state variable as "key".再添加一个 state 变量作为“键”。

render() {
let messages = this.state.messages.map((item, key) =>
  <div>
    <ul key={item.id} className="messageList">
      <li><h3 className="messageTitle">{item.title}</h3></li>
      <li>{item.message}</li>
      <li className="messageSignature">from: {item.from}</li>
    </ul>
    {this.state.showSendMessage && this.state.key == key ? <SendMessage recipient={item.from} title={item.title}></SendMessage> : null}
    <button onClick={this.toggleShowSendMsg.bind(key)}>Answer</button>
  </div>
);

and change the state of key in your toggleShowSendMsg method the way you are doing for showSendMessage.并按照您对 showSendMessage 所做的方式更改您的 toggleShowSendMsg 方法中的密钥 state。

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

相关问题 如何将 React 组件渲染到主 App 组件中/ - How do I render a React component into the main App component/ 如何在React中使用axios在一个单独的组件中渲染一个结果? - How do I only render one result in a separate component using axios in React? 如何在 React(Next.js) 的一个组件中的一个文件夹中呈现文件夹? - How do I render folders within a folder in one component in React(Next.js)? 如何在反应中重新呈现相同的组件页面? - How can I re render same component page in react? 我如何通过链接传递其状态在一个组件中设置的对象的属性,并在 React 中的另一个组件中呈现它们 - How do i pass the properties of an object whose state is set in one component through a link and render them in another component in React 如何根据用户当前所在的页面呈现相同的组件? - How do I render same component depending on the page the user is currently on? 如何在React组件中将字符串呈现为子项? - How do I render a string as children in a React component? React Router,如何告诉路由器渲染到组件? - React Router, how do I tell the router to render to a component? 如何使用 React 中的条件在组件内渲染 3 个 SVG 中的 1 个? - How do I render 1 of 3 SVGs inside a component using conditions in React? 我如何安全地从 React 组件渲染 Markdown? - How do I SAFELY render Markdown from a React component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM