I have this Note element in React to show saved strings input by user from a text area. Somehow, new line is not reflected and replaced by space.
I have checked the strings before it's mapped to paragraph, the string still have the new lines characteres. But when it's mapped to paragraph, the new lines are replaced by spaces.
How to make the new lines remain reflected on the new created paragraph elements? Below is my code.
class Note extends React.Component {
render() {
return (
this.props.notes.map((note, index) =>
<div className="note-important" key={index}>
<p>{note}</p>
</div>
)
);
}
}
class NoteAndDone extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
notes: [],
}
this.handleClick = this.handleClick.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({
input: e.target.value
})
}
handleClick(e) {
let input = document.getElementById('textarea-note').value;
if (input && !/^\s*$/.test(input)) {
this.setState({
notes: this.state.notes.concat(this.state.input)
});
}
}
render() {
return (
<div className="NoteAndDone">
<div className="Title">Note</div>
<textarea id="textarea-note" onChange={this.handleChange} placeholder='Note important things here...' />
<div className="button-save-container">
<button className="btn btn-primary" id="button-save-note" onClick={this.handleClick}>Save</button>
</div>
<Note notes={this.state.notes}/>
</div>
);
}
}