简体   繁体   English

将子(评论)部分添加到 React.js 中的每个父(答案)组件

[英]Adding Child (Comment) part to every Parent (Answer) Component in React.js

import React, { useState, useEffect } from 'react'
import { Link } from 'react-router-dom'
import moment from 'moment'
import Avatar from '../../components/Avatar/Avatar'
import { useSelector, useDispatch} from 'react-redux'
import { useParams } from 'react-router-dom'
import { deleteAnswer } from '../../actions/question'

const DisplayAnswer = ( { question, handleShare }  ) => {

    const User = useSelector((state) => (state.currentUserReducer))
    const dispatch = useDispatch()
    const { id } = useParams()
    
    const [button, setButton] = useState(false);

    const handleDelete = (answerId, noOfAnswers) => {
        dispatch(deleteAnswer(id, answerId, noOfAnswers-1))
    }

    const handleComment = (e) => {
        setButton(!button)
        alert(e.target.id)
    }

    return (
        <div>
            {   
                question.answer.map( (ans) => (
                    <div className="display-ans" key={ans._id}>
                        <p>{ans.answerBody}</p>
                        <div className="question-actions-user">
                            <div>
                                <button type="button" onClick={handleShare}>Share</button>
                                {          
                                    User?.result?._id === ans?.userId && (
                                        <button type='button' onClick={ () => handleDelete(ans._id, question.noOfAnswers) }>Delete</button>
                                    )
                                }

                                <div>
                                        
                                </div>

                                <button id = {ans._id} type='button' onClick = { (e) => handleComment(e) }> Add Comment </button>

                                {
                                    button && 
                                    (
                                        <div id = {ans._id}>
                                            <textarea rows='5' cols='30'> </textarea> <br />
                                            <button type='button'> Post </button>
                                        </div>
                                    )
                                }
                                
                            </div>

                            <div>
                                <p>answered { moment(ans.answeredOn).fromNow()}</p>
                                <Link to={`/Users/${ans.userId}`} className='user-link' style={{color:'#0086d8'}}>
                                    <Avatar backgroundColor="lightgreen" px='8px' py='5px' borderRadius='4px'>{ans.userAnswered.charAt(0).toUpperCase()}</Avatar>
                                    <div>
                                        {ans.userAnswered}
                                    </div>
                                </Link>
                            </div>

                        </div>

                   </div> 

                ))
            }
        </div>


    )
}

export default DisplayAnswer

I want to add a comment part under every answer to do that i added a " Add Comment " button under every Answer and i have a button click on that button我想在每个答案下添加一个评论部分,我在每个答案下添加了一个“添加评论”按钮,我有一个按钮单击该按钮

and what i want is whenever the button is clicked the addcomment (textbox) should be added under it我想要的是每当单击按钮时,应在其下添加添加注释(文本框)

but when i click the button the addcomment (textbox) is getting added under every answer但是当我单击按钮时,每个答案下都会添加添加注释(文本框)

like if 10 answers are their then Addcommment box is getting added under every 10 answers就像如果他们有 10 个答案,那么每 10 个答案就会添加一个 Addcommment 框

Each answer must have his own 'state' to display his own textArea, so you have to extract the code of the 'answer' in a new Answer component, and render a new component in the map method.每个答案都必须有自己的“状态”才能显示自己的文本区域,因此您必须在新的答案组件中提取“答案”的代码,并在 map 方法中呈现新组件。

Each Answer will thus use a "useState" with a "[isTextAreaVisible, setIsTextAreaVisible] = useState(false);"因此,每个答案都将使用带有“[isTextAreaVisible, setIsTextAreaVisible] = useState(false);”的“useState” state. state。

Currently there is only a single button state that all the mapped answers render a button for.目前只有一个button state所有映射的答案都为其呈现一个按钮。 A simple solution would be to instead store the answer id of the answer you want to add a comment for.一个简单的解决方案是存储您要为其添加评论的答案的答案 ID。

Example:例子:

const DisplayAnswer = ({ question, handleShare }) => {
  ...

  const [commentId, setCommentId] = useState(null); // <-- initially null

  ...

  const handleComment = (e) => {
    setCommentId(e.target.id); // <-- set answer id here
    alert(e.target.id);
  };

  return (
    <div>
      {question.answer.map((ans) => (
        <div className="display-ans" key={ans._id}>
          <p>{ans.answerBody}</p>
          <div className="question-actions-user">
            <div>
              ...

              <button
                id={ans._id}
                type="button"
                onClick={handleComment}
              >
                Add Comment
              </button>

              {commentId === and._id && ( // <-- conditionally render match by id
                <div id={ans._id}>
                  <textarea rows="5" cols="30" />
                  <br />
                  <button type="button">Post</button>
                </div>
              )}
            </div>

            ...
          </div>
        </div>
      ))}
    </div>
  );
};

When the "Post comment" button is clicked and the entered comment is handled don't forget to also set the commentId value back to null to conditionally hide the input.当单击“发表评论”按钮并处理输入的评论时,不要忘记将commentId值设置回 null 以有条件地隐藏输入。

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

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