[英]Everything works fine but when I click edit button, a comma (,) is add every time. I would not find what going wrong?
I am trying to build a note taking app. 我正在尝试建立一个笔记应用程序。 I have created Note And Board component.
我创建了Note And Board组件。 Everything works fine but when I click edit button, a comma (,) is add every time.
一切正常,但当我点击编辑按钮时,每次都会添加一个逗号(,)。 I would not find what's going wrong?
我不会发现出了什么问题?
Here's my code: 这是我的代码:
class Note extends React.Component{ constructor(){ super(); this.state={ editing:true } } edit(){ this.setState({editing: false}); } save(){ var val =this.refs.newText.value; console.log(this.refs.newText.value) this.props.onChange(val,this.props.index) this.setState({editing: true}); } delete(){ this.props.onRemove(this.props.index) } renderDisplay(){ return( <div className="note"> <p>{this.props.children}</p> <span> <button onClick={this.edit.bind(this)}>Edit</button> <button onClick={this.delete.bind(this)} className="danger">Delete</button> </span> </div> ); } renderForm(){ return( <div className="note"> <textarea ref="newText" defaultValue={this.props.children}></textarea> <button onClick={this.save.bind(this)}>Save </button> </div> ); } render(){ // this.state.editing ? return this.renderDisplay() : return this.renderForm(); if(this.state.editing){ return this.renderDisplay(); }else{ return this.renderForm(); } } } class Board extends React.Component{ constructor(){ super(); this.state={ notes: ["Hello"] } } update(newText,i){ var arr = this.state.notes; arr[i]=newText; this.setState({notes:arr}); } remove(i){ var arr =this.state.notes; arr.splice(i,1); this.setState({notes:arr}); } eachNote(note,i){ return( <Note key={i} index={i} onChange={this.update.bind(this)} onRemove= {this.remove.bind(this)}> {note}</Note> ) } add(text){ // alert("hello"); var arr = this.state.notes; arr.push(text); this.setState({notes:arr}); } render(){ return <div className="board"> <button onClick={this.add.bind(this,"New Note")}>Add</button><br/> { this.state.notes.map(this.eachNote.bind(this)) } </div> } } ReactDOM.render(<Board ></Board>, document.getElementById('root'));
*{ box-sizing: border-box; } body,html{ margin:0; padding: 0; height:100%; } .board{ background:#fbd14b; height:100%; min-height: 100vh; width:100%; padding:20px; } .note{ background:yellow; color: #333; width: 200px; padding: 10px; margin-bottom: 20px; box-shadow: 0px 0px 15px 5px rgba(0,0,0,.1); display: inline-block; margin-right: 20px; vertical-align: top; min-height: 150px; } button{ border:0; box-shadow: none; padding: 5px 10px; background:#3ac569; color: #fff; margin:0 5px 5px 0; } button:hover{ cursor: pointer; } .danger{ background: #E71D36; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div>
It's happening because of the space you provide before {note}
, 这是因为你在
{note}
之前提供的空间,
<Note key={i} index={i} onChange={this.update.bind(this)} onRemove= {this.remove.bind(this)}> {note}</Note>
that changes children
to Note
in an array, 改变
children
到Note
一个数组,
[' ', 'Hello']
So when you do defaultValue = {this.props.children}
, it converts array
to string
by performing this.props.children.join(',')
and that's how you are getting ,
in your result. 所以,当你
defaultValue = {this.props.children}
,将其转换array
到string
通过执行this.props.children.join(',')
这就是你如何得到,
在您的结果。
You can fix this by removing space before {Note}
您可以通过在
{Note}
之前删除空格来解决此问题
class Note extends React.Component{ constructor(props){ super(); this.state={ editing:true } } edit(){ this.setState({editing: false}); } save(){ var val =this.refs.newText.value; console.log(this.refs.newText.value) this.props.onChange(val,this.props.index) this.setState({editing: true}); } delete(){ this.props.onRemove(this.props.index) } renderDisplay(){ return( <div className="note"> <p>{this.props.children}</p> <span> <button onClick={this.edit.bind(this)}>Edit</button> <button onClick={this.delete.bind(this)} className="danger">Delete</button> </span> </div> ); } renderForm(){ return( <div className="note"> <textarea ref="newText" defaultValue={this.props.children}></textarea> <button onClick={this.save.bind(this)}>Save </button> </div> ); } render(){ // this.state.editing ? return this.renderDisplay() : return this.renderForm(); if(this.state.editing){ return this.renderDisplay(); }else{ return this.renderForm(); } } } class Board extends React.Component{ constructor(){ super(); this.state={ notes: ["Hello"] } } update(newText,i){ var arr = this.state.notes; arr[i]=newText; this.setState({notes:arr}); } remove(i){ var arr =this.state.notes; arr.splice(i,1); this.setState({notes:arr}); } eachNote(note,i){ return( <Note key={i} index={i} onChange={this.update.bind(this)} onRemove= {this.remove.bind(this)}>{note}</Note> ) } add(text){ // alert("hello"); var arr = this.state.notes; arr.push(text); this.setState({notes:arr}); } render(){ return <div className="board"> <button onClick={this.add.bind(this,"New Note")}>Add</button><br/> { this.state.notes.map(this.eachNote.bind(this)) } </div> } } ReactDOM.render(<Board ></Board>, document.getElementById('root'));
*{ box-sizing: border-box; } body,html{ margin:0; padding: 0; height:100%; } .board{ background:#fbd14b; height:100%; min-height: 100vh; width:100%; padding:20px; } .note{ background:yellow; color: #333; width: 200px; padding: 10px; margin-bottom: 20px; box-shadow: 0px 0px 15px 5px rgba(0,0,0,.1); display: inline-block; margin-right: 20px; vertical-align: top; min-height: 150px; } button{ border:0; box-shadow: none; padding: 5px 10px; background:#3ac569; color: #fff; margin:0 5px 5px 0; } button:hover{ cursor: pointer; } .danger{ background: #E71D36; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div>
Here you go with the solution in the jsfiddle https://jsfiddle.net/6ktw0ucm/1/ 在这里,您可以使用jsfiddle中的解决方案https://jsfiddle.net/6ktw0ucm/1/
renderForm(){
return(
<div className="note">
<textarea ref="newText" defaultValue={this.props.children[1]}></textarea>
<button onClick={this.save.bind(this)}>Save </button>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.