【发布时间】:2019-01-17 15:02:44
【问题描述】:
我在 React 中有这个 Note 元素来显示用户从文本区域输入的保存的字符串。不知何故,新行没有被空间反射和取代。
我在映射到段落之前检查了字符串,字符串仍然有换行符。但是当它映射到段落时,新行会被空格替换。
如何使新行仍然反映在新创建的段落元素上?下面是我的代码。
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>
);
}
}
【问题讨论】:
标签: javascript html string reactjs newline