javascript - 不同条件下的 React 调用方法

标签 javascript reactjs methods

我是 React 初学者,我编写了以下代码:

class Note extends React.Component {
   constructor(props) {
       super(props);
       this.state = {editing: false};

        this.edit = this.edit.bind(this);
        this.save = this.save.bind(this);

   }

   edit() {
       // alert('edit');
       this.setState({editing: !this.state.editing});
   }

   save() {
       this.props.onChange(this.refs.newVal.value, this.props.id);
       this.setState({editing: !this.state.editing});
       // console.log('save is over');
   }

   renderForm() {
       return (
           <div className="note">
               <textarea ref="newVal"></textarea>
               <button onClick={this.save}>SAVE</button>
           </div>
       );
   }

   renderDisplay() {
       return (
           <div className="note">
               <p>{this.props.children}</p>
               <span>
                         <button onClick={this.edit}>EDIT</button>
                        <button onClick={this.remove}>X</button>
               </span>
           </div>
       );
   }

   render() {
       console.log(this.state.editing);
       return (this.state.editing) ? this.renderForm()
           : this.renderDisplay()

   }
}

class Board extends React.Component {

   constructor(props){
       super(props);

       this.state = {
           notes: []
       };

       this.update = this.update.bind(this);
       this.eachNote = this.eachNote.bind(this);
       this.add = this.add.bind(this);
   }

   nextId() {
       this.uniqeId = this.uniqeId || 0;
       return this.uniqeId++;
   }

   add(text) {
       let notes = [
           ...this.state.notes,
           {
               id: this.nextId(),
               note: text
           }
       ];

       this.setState({notes});
   }

   update(newText, id) {
       let notes = this.state.notes.map(
           note => (note.id !== id) ?
               note :
               {
                   id: id,
                   note: newText
               }
       );
       this.setState({notes})
   }

    eachNote(note) {
       return (<Note key={note.id}
                     id={note.id}
                     onChange={this.update}>

           {note.note}
       </Note>)
   }

   render() {
       return (<div className='board'>
           {this.state.notes.map(this.eachNote)}
           <button onClick={() => this.add()}>+</button>
       </div>)
   } 
}

ReactDOM.render(<Board />,
   document.getElementById('root'));

在render()中,onClick事件有一个功能,就是如果这样使用:{this.add}会产生如下错误:

Uncaught Error: Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, view, detail, ...})

为什么?而在 eachNote() 方法中使用此命令:

onChange={this.update}

而且没有错误。

谁能告诉我原因?谢谢。

最佳答案

问题是在 add 函数中,您使用参数文本并将其设置为状态,因此当您调用 onClick={() => this.add()} 时,您是不向 add 函数传递任何参数,因此在其定义中 text 未定义,因此 state note 设置为未定义。

但是,如果您直接调用它,如 onClick={this.add}add 函数会接收 event 对象作为参数,并且因此它将 state note 设置为您用来render

的事件对象

关于javascript - 不同条件下的 React 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209510/

相关文章:

javascript - 使用javascript输入事件获取关键代码

javascript - 我怎样才能使元素居中

javascript - 提交事件可以取消吗?

reactjs - react 路由器|私有(private)路由器上的调用功能

javascript - 不同位置的过渡元素

reactjs - React + Redux - 为什么不连接所有组件?

javascript - 使用 MySQL 数据库填充 DataTable

c++ - 递归方法 C++

java - 在 Java 中返回字符串方法?

c++ - 空类方法实现