I have an app that reads <input type="text"> and let people comment on that word. I want to do is save this comment to a file and retrieve the data when I search the same name again.
eg. I input for "fish". App3 will render and shows where to comment, as I submit my comment a new file will create (eg. fish.txt) somewhere. And when I visit the word again, I can read my past comments.
This is my code so far
class App3 extends Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {items: [], text: ''};
}
render() {
return (
<div className="app3">
<h1>Comments</h1>
<div className="commentsSection">
<AddComment items={this.state.items} />
</div>
<form onSubmit={this.handleSubmit}>
<textarea onChange={this.handleChange} value={this.state.text} />
<button>Comment</button>
</form>
</div>
);
}
handleChange(e) {
this.setState({text: e.target.value});
}
handleSubmit(e) {
e.preventDefault();
var newItem = {
text: this.state.text,
id: Date.now()
};
this.setState((prevState) => ({
items: prevState.items.concat(newItem),
text: ''
}));
}
}
class AddComment extends React.Component {
render() {
return (
<ul>
{this.props.items.map(item => (
<p key={item.id}>{item.text}</p>
))}
</ul>
);
}
}