如何在React的.map函数中仅修改一个元素?

7

我对React还不太熟悉,在这里有些迷茫。我正在从Firebase中加载数据,并使用.map函数呈现该对象的props,以列出页面上的所有“评论”,这很好用。我还想调用一个组件,使用户可以回复单个评论(即映射中的一个特定元素),但是由于此代码的预期,当单击回复按钮时,它会在映射的每个元素下显示被调用的组件,这是不可取的。在这种情况下,是否有任何方法可以仅为映射的一个元素调用组件?

changeIsReply() {
  this.setState(
    {isReply: !this.state.isReply,}
  );
}    

  render() {

  return (
    <div>
    <ul className="list-group">
    {Object.values(this.props.commentInfo).map((data) =>
      data.map((secondData, i) =>
        <div className="commentHolder" key={i}>
            <p>{secondData.text}</p>
            <p>{secondData.category}</p>
            <p>{secondData.organization}</p>
            <button> UpButton </button> <br />
            <button> DownButton </button>
            <button onClick = {this.changeIsReply} > Reply </button>
            {this.state.isReply ? <SetComment id={secondData.key} /> : null}
        </div>
      )
    )}
    </ul>
    </div>
  )
}
4个回答

5

这是因为您正在为所有评论使用单个状态。

<button onClick = {() => this.changeIsReply(secondData.key)} > Reply </button>

{this.state.isReply && this.state.clickedComment == {secondData.key} ? <SetComment id={secondData.key} /> : null}

并将您的changeIsReply()更改为:

changeIsReply(clickedId) {
    this.setState({
        isReply: !this.state.isReply, 
        clickedComment: clickedId
    });
}

请检查这是否有效。不要忘记将新状态添加到您的构造函数中。


0

你需要保持isReply是一个数组的数组,并根据点击的评论的索引仅更新正确的值,类似于以下方式:

state= {
   isReply: [[]]
}
changeIsReply(i, j) {
  var isReply = [...this.state.isReply]
  if(isReply[i] === undefined) {
    isReply.push([true]) = 
  } else {
    if(isReply[i][j] === undefined) {
      isReply[i].push(true)
    } else {
       isReply[i][j] = !isReply[i][j]
    }
  }
  this.setState(
    {isReply}
  );
}    

  render() {

  return (
    <div>
    <ul className="list-group">
    {Object.values(this.props.commentInfo).map((data, i) =>
      data.map((secondData, j) =>
        <div className="commentHolder" key={j}>
            <p>{secondData.text}</p>
            <p>{secondData.category}</p>
            <p>{secondData.organization}</p>
            <button> UpButton </button> <br />
            <button> DownButton </button>
            <button onClick = {() => this.changeIsReply(i, j)} > Reply </button>
            {this.state.isReply[i][j] ? <SetComment id={secondData.key} /> : null}
        </div>
      )
    )}
    </ul>
    </div>
  )
}

0
我建议将其拆分为两个组件:评论列表 (CommentList) 和评论 (Comment) 组件。

comment-list.js

import Comment from './comment';

class CommentList extends Component{
    render(){
    return(
        <div>
        <ul className="list-group">
                {Object.values(this.props.commentInfo).map((data) =>
                data.map((secondData, i) =>
                    <Comment text={secondData.text} category={secondData.category} organization={secondData.organization} key={secondData.key} />
      )
        </ul>
      </div>
    )
  }
}

comment.js

class Comment extends Component {
    constructor(props){
    super(props);
    this.state = {
        isReply: false
    }
  }
  changeIsReply = () => {
    this.setState(
      {isReply: !this.state.isReply,}
    );
  }   
    render(){
    const {text, category, organization, key} = this.props;
    return(
      <div className="commentHolder" key={i}>
        <p>{text}</p>
        <p>{category}</p>
        <p>{organization}</p>
        <button> UpButton </button> <br />
        <button> DownButton </button>
        <button onClick = {this.changeIsReply} > Reply </button>
        {this.state.isReply ? <SetComment id={key} /> : null}
      </div>
    )
  }
}

export default Comment;

这样每个注释都可以控制自己的状态,以及是否显示 SetComment 组件。

尝试创建更多组件,而不是将所有标记放在单个渲染方法中。这还有助于组织代码,使您将每组值映射到一个组件,我认为这使得理解起来更容易。


0

您可以跟踪哪些评论应该使用SetComment组件。

constructor(props) {
  super(props);
  this.state = {
    // init commentReplyIds as empty array
    commentReplyIds : [],
    // ... rest of your state
  }
}

changeIsReply(commentId) {
  const newCommentReplyIds = [...this.state.commentReplyIds. commentId];
  this.setState(
    {commentReplyIds: newCommentReplyIds}
  );
}    

  render() {
  // in here I am using the data.text as the unique id of each comment, you can use other serializable value.
  return (
    <div>
    <ul className="list-group">
    {Object.values(this.props.commentInfo).map((data) =>
      data.map((secondData, i) =>
        <div className="commentHolder" key={i}>
            <p>{secondData.text}</p>
            <p>{secondData.category}</p>
            <p>{secondData.organization}</p>
            <button> UpButton </button> <br />
            <button> DownButton </button>
            <button onClick = {() => this.changeIsReply(secondData.text)} > Reply </button>
            {this.state.commentReplyIds.includes(secondData.text) ? <SetComment id={secondData.key} /> : null}
        </div>
      )
    )}
    </ul>
    </div>
  )
}

通过这种方法,您可以拥有多个SetComment组件,位于用户单击回复评论按钮的不同评论中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接