简体   繁体   English

我不知道为什么我的组件不呈现更新状态。 即使状态被更新并反映在数据库中

[英]I can't figure out why my component does not rendered with the updated state. Even though, the state is updated and reflected in the database

So I'm working on a simple CRUD note app. 因此,我正在开发一个简单的CRUD note应用程序。 I ran into this one problem when my component goes into edit mode and when editing is done. 当我的组件进入编辑模式并且完成编辑时,我遇到了这个问题。 The PUT request successfully update the content in the db and the edit mode component will return back to a normal note. PUT请求成功更新了数据库中的内容,并且编辑模式组件将返回到正常注释。 Here are the related code: 以下是相关代码:

constructor(props) {
    super(props);

    this.state = {
      notes: [],
      id: null,
      title: "",
      content: "",
      toEdit: null
    }

    this.handleTitleChange = this.handleTitleChange.bind(this);
    this.handleContentChange = this.handleContentChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.deleteNote = this.deleteNote.bind(this);
    this.handleEditSubmit = this.handleEditSubmit.bind(this);
  }
  handleContentChange = (event) => {
    this.setState({ content: event.target.value});
  }

Here's the update function: 这是更新功能:

// Handle new content edit
  handleEditSubmit = (noteId) => {
    console.log(noteId)
    axios.put('http://localhost:3000/notes/' + noteId, 
    {
      content: this.state.content
    })
     .then(res => {
      // Rerender the editNote into a normal note.
      this.setState({ 
      toEdit: null,
     }, () => {console.log(this.state.content, this.state.toEdit)}) 
      console.log(res);
    })
     .catch(err => {
      console.log(err);
      this.setState({ toEdit: null })
     })
  }

Lastly, render normal note component when toEdit is null and render edit-mode component when toEdit===index: 最后,当toEdit为null时,呈现普通笔记组件,而当toEdit === index时,呈现编辑模式组件:

{this.state.notes.map((output, index) => {
            if (this.state.toEdit === index) {
              return (
                <div key={index} className="note">
                  <div className="title-container"><h4>{output.title}</h4></div>
                    <textarea 
                      className="textarea"
                      type="text" 
                      name="edit-content" 
                      defaultValue={output.content} 
                      onChange={this.handleContentChange} 
                    />
                    <Button bsStyle="danger" type="button" onClick={this.handleCancel} >Cancel</Button>
                    <Button bsStyle="primary" type="button" onClick={this.handleEditSubmit.bind(this, output.id)} >Done</Button>
                </div>
              )
            } else {
              return (
                <div key={index} className="note">
                  <div className="title-container"><h4>{output.title}</h4></div>
                    <p>{output.content}</p>
                    <Button bsStyle="danger" onClick={this.deleteNote.bind(this, index, output.id)}>Delete</Button>
                    <Button bsStyle="primary" onClick={this.edit.bind(this, index)} >Edit</Button>
                </div>
              )
            }
          }

The edited text won't show after PUT request and after re-render. 在PUT请求之后和重新呈现之后,编辑后的文本将不会显示。 Have to refresh page to get the updated note. 必须刷新页面以获取更新的注释。 Have a look at the gif: https://giphy.com/gifs/dn15rkILhjMyvVl8CX/fullscreen 看看gif: https//giphy.com/gifs/dn15rkILhjMyvVl8CX/fullscreen

You are not changing anything in notes[], as you are using {this.state.notes.map((output, index) => { to render notes.You should updated the notes[] entity after axios done. You can do something like this 您没有更改notes []中的任何内容,因为您正在使用{this.state.notes.map((output, index) => {来呈现注释。应该在axios完成后更新notes []实体。像这样的东西

 handleEditSubmit = (noteId) => {
    console.log(noteId)
    axios.put('http://localhost:3000/notes/' + noteId, 
    {
      content: this.state.content
    })
     .then(res => {
      // Rerender the editNote into a normal note.
      this.setState({ 
      toEdit: null,
      notes:res.notes // this is changed part
     }, () => {console.log(this.state.content, this.state.toEdit)}) 
      console.log(res);
    })
     .catch(err => {
      console.log(err);
      this.setState({ toEdit: null })
     })
  }

note: notes:res.notes // this is changed part changed 注意: notes:res.notes // this is changed part更改

so, after this change this.state.notes will have new updates and you can easily bind that. 因此,此更改之后,this.state.notes将具有新的更新,您可以轻松地进行绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM