P
P
PlasterTom2018-04-07 19:01:09
React
PlasterTom, 2018-04-07 19:01:09

Why are key and refs needed in this code?

Actually, the question is in the title. Why are key and refs needed in CommentList? And are they needed, like, and without them everything works. Full code on github link

class Article extends Component {
    
    static propTypes = {
        text: PropTypes.string,
        date: PropTypes.string
    };

state = {
    updateIndex: 0
}
    handleDelete = () => {
        const {deleteArticle, article} = this.props
        deleteArticle(article.id);
}

    render(){
        return(
            <div className='article card' style={cardStyle}>
                <p>{this.props.text}</p>
                <p>{this.props.date}</p>
                <span style={btnDelete} onClick={this.handleDelete}>X</span>
                <CommentList article={this.props.article} ref = {this.setCommentsRef} key = {this.state.updateIndex}/>
        </div>
        )
    }

 setCommentsRef = ref => {
        this.comments = ref
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zvezdocheteg, 2018-04-07
@PlasterTom

Key is not used in the CommentList component, so this is an extra props.
ref -> this.comments is also only available in the Article scope but it is not used there, so these 2 props are superfluous.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question