Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

javascript - React - Cannot read property 'call' of undefined

Everything seems to work with this small app except adding a new note. Button is located on the Board component.

i know this problem is usually caused by not binding value of 'this' properly. I'm not sure if that's the issue here or if i'm missing something else. Thanks

Demo: http://jsbin.com/pewahi/edit?js,output

/* jshint asi:true */

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

  render() {
    if (this.state.editing) {
      return this.renderForm()
    } else {
      return this.renderDisplay()
    }
  }

  edit() {
    this.setState({editing: true})
  }

  save() {
    this.props.changeHandler(this.refs.newText.getDOMNode().value, this.props.index)
    this.setState({editing: false})
  }

  remove() {
    this.props.removeHandler(this.props.index)
  }

  renderDisplay() {
     return (      
      <div className="note">
        <p>{this.props.children}</p>
        <span>
          <button className="btn btn-sm glyphicon glyphicon-pencil" onClick={this.edit.bind(this)}></button>
          <button className="btn btn-sm glyphicon glyphicon-trash" onClick={this.remove.bind(this)}></button>
        </span>
      </div>   
    )    
  }

  renderForm() {
    return (
      <div className="note">
        <textarea ref="newText" defaultValue={this.props.children} className="form-control"></textarea>
        <button onClick={this.save.bind(this)} className="btn btn-success btn-sm"><span className="glyphicon glyphicon-floppy-disk"></span> Save</button>
      </div>
    )
  }
}

Note.propTypes = { 
  editing: React.PropTypes.bool,
  onChange: React.PropTypes.func,
  onRemove: React.PropTypes.func
}

Note.defaultProps = { editing: false }

class Board extends React.Component {
  constructor(props) {
    super(props)
    this.state = { 
      notes: [{note: 'hi', id: this.nextId()}]
    }
  }

  update(newText, i) {
    var arr = this.state.notes
    arr[i].note = newText
    this.setState({notes: arr})
  }

  remove(i) {
    var arr = this.state.notes
    arr.splice(i, 1)
    this.setState({notes: arr})
  }

  addNote(text) {
    var arr = this.state.notes
    arr.push({
      id: this.nextId(),
      note: text
    })
    console.log(arr)
    this.setState({notes: arr})
  }

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


  eachNote(note, i) {
    return (
      <Note key={note.id}
            index={i}
            changeHandler={this.update.bind(this)}
            removeHandler={this.remove.bind(this)}

        >{note.note}
      </Note>
    )
  }

  render() {
    return (
      <div className="board">
        {this.state.notes.map(this.eachNote, this)}
        <button onClick={this.addNote.bind(this, "new note")} className="btn btn-success btn-sm glyphicon glyphicon-plus"></button>
      </div>
    )
  }
}

React.render(
  <Board />,
  document.getElementById('message-board')
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is fine. This is likely a bug with JSBin, and how it handles transpilation with Babel. If you add the pragma // noprotect to the top of your code you will see that it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...