简体   繁体   English

一切正常,但当我点击编辑按钮时,每次都会添加一个逗号(,)。 我不会发现出了什么问题?

[英]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, 改变childrenNote一个数组,

[' ', '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} ,将其转换arraystring通过执行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.

相关问题 JQUERY 仅在第一次单击按钮时显示叠加。 怎么了? - JQUERY Show overlay when button click work only first time. What's wrong? 我如何使三个圆圈交替出现而又不能同时进行。 不要使用CSS - How would I make the 3 circles alternate without them going on at the same time. DON'T USE CSS 当我第一次点击删除按钮时,所有的笔记都消失了,但是当我刷新页面时它工作得很好 - When I first click the delete button, all the notes are disappeared but, when I refresh the page it works just fine 为什么是 <tfoot> 每次我单击按钮时添加? - Why is <tfoot> added every time when I click on the button ? 当我单击按钮时,每5个项目(div)添加一个班级 - add class every 5 items(div) when i click on button 测量反应时间的简单按钮点击游戏。 我如何使用javascript节省最快的时间 - Simple button click game that measures reaction time. How do i save the fastest time with javascript 当我点击提交按钮我的javascript第一个警报框显示在移动设备但在桌面上工作正常 - when I click on submit button my javascript first alert box show in mobile device but works fine in desktop CKEditor SetData()仅在第一次使用。 我在想什么 - CKEditor SetData() only works first time. What am I missing 这有什么问题? 当我将鼠标悬停在光标上时试图播放声音,但仅在单击时才起作用? - what is wrong with this? trying to play a sound when i hover with my curser, but works only when i click? 我如何找到我的代码出了什么问题 - how do i find what's going wrong with my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM