Pretty straight forwards really...I know that it's usually bad practice to repeat your code so I'm wondering how I could join handleAuthorChange() and handleContentChange() into one method. I guess it was hard for me to find a solution because I don't understand how I would know which part of the state I'm updating. I was thinking maybe embed a HTML id and check on the event target if IDs match but that seems pretty inefficient. Thanks in advance!
import React, { Component } from 'react';
class AddComment extends Component {
state = {
author: '',
content: ''
}
handleAuthorChange = (e) => {
this.setState({
author: e.target.value
});
}
handleContentChange = (e) => {
this.setState({
content: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
this.props.addComment(this.state);
this.setState({
author: '',
content: ''
});
}
render() {
return (
<div className="add-form">
<form onSubmit={ this.handleSubmit } className="card hoverable p-bottom">
<p className="p-top">ADD COMMENT</p>
<input onChange={ this.handleContentChange } value={this.state.content} type="text" placeholder="comment" />
<input id="name" onChange={ this.handleAuthorChange } value={this.state.author} type="text" placeholder="name" />
<input value="submit" type="submit"/>
</form>
</div>
)
}
}
export default AddComment;