1

I have CommentSection component right here

    export class CommentSection extends Component {

    <...> some stuff that is not related to question    

    render() {
        if (!this.state.data || !this.state.data.length)
            return null;     
        var comments = [];
        for (let i = 0; i < this.state.data.length; i++) {
            var item = this.state.data[i]; // single comment
            var commentReplies = this.pushReplies(item.id, i);                      
            if (item.replyToId == null) {
                comments.push( // pushing comment if it has replies
                    <React.Fragment key={'b' + i}>
                        <div className="comment">
                            <p className="comments_username">{item.userName}</p>
                            <p className="comments_body">{item.body}</p>
                            <p className="comments_date"> {item.publicationDate}</p>
                            <p className="reply">Reply</p>
                        </div>                        
                        <SmallCommentForm postId={this.state.id} replyToId={item.id} />

                        {commentReplies}

                    </React.Fragment>
                );
            } 

        }

        return (
            <React.Fragment>
                {comments}
            </React.Fragment >
        );
    }
}

I need to toggle <SmallCommentForm> component, but I can't correctly see how I can do it. Should I somehow add/remove display:none attributes from document.getElementById(replyToId) or what?

Almost every solution that I've seen contains if statement which decides should it render code or not. But I can't apply this kind of solution in that case: all comments have only one state, because I am using for () {}

Is there any solutions or am I doing it all wrong? Sorry for my English, it's not my primary language.

1 Answer 1

1

I think you can have another attribute for toggling the <SmallCommentForm /> So you have your data in an array in this.state and you have another attribute for the toggle and do { this.state.isOn && <SmallCommentForm />}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.