setState()不触发重新渲染

setState()不触发重新渲染

问题描述:

这段代码有什么内在的错误吗?setState()不触发重新渲染

部分的问题是这样的:

// Update state and trigger re-render 
// NOT WORKING 
this.setState({ subsections }); 

国家没有被置位,当我打电话的setState()。尽管数据存在于subsections对象中。我在其他方法上使用了相同的代码模式,并按预期工作。

数据也保存在Parse中,如果我刷新它的页面。

import React from "react"; 
import Parse from 'parse'; 

import Subsections from './Subsections'; 
import AddSubsectionForm from './AddSubsectionForm'; 

class Manage extends React.Component { 
    constructor() { 
    super(); 

    this.addSubsection = this.addSubsection.bind(this); 

    // Set initial state 
    this.state = { 
     subsections: {} 
    }; 
    } 

    addSubsection(subsection) { 
    // Make copy of state 
    const subsections = {...this.state.subsections}; 

    // Create new object 
    var SubsectionTest = Parse.Object.extend('SubsectionTest'); 
    var subsectionTest = new SubsectionTest(); 

    // Save object, then update state on return 
    subsectionTest.save({ 
     name: subsection.name, 
     description: subsection.description 
    }).then(function(newSubsection) { 
     console.log('Has saved id of: '+newSubsection.id); 

     // Add new subsection to local state 
     subsections[newSubsection.id] = subsection; 

     // Log updatd subsections object 
     console.log(subsections); 

     // Update state and trigger re-render 
     // NOT WORKING 
     this.setState({ subsections }); 
    }, 
    function(error) { 
     console.log('Error:'); 
     console.log(error); 
    }); 
    } 

    /* 
    Loads subsections from Parse and displays them. 
    Code removed for clarity. 
    */ 
    componentWillMount() { 
    this.loadMenuItems(); 
    } 

    render() { 
    if (this.state.subsections) { 
     return (
     <div className="subsections"> 
      <div className="mt3 border-top"> 
      <h3 className="mt1 bold s2">Subsections</h3> 

      <div className="mt1"> 
       <Subsections subsections={this.state.subsections} /> 
      </div> 
      </div> 

      <div className="mt3 border-top"> 
      <AddSubsectionForm addSubsection={this.addSubsection}/> 
      </div> 
     </div> 
    ) 
    } 
    } 
} 

export default Manage; 
+0

你应该'else {return null; }'在你的render()函数中(不是答案,我知道它只是伸出在我身上) – rossipedia

您的问题是this未在承诺的then处理程序的约束:

}).then(function(newSubsection) { 

由于您使用的ES2015班,你大概可以使用箭头功能(结合this其词法范围):

}).then(newSubSection => { 

另一种选择是将其明确绑定:

}).then(function(newSubsection) { 
... 
}.bind(this)); 
+0

我实现了箭头函数,但它仍然不更新状态。我忘了提及我之前已经明确地将它像第二个例子那样绑定了。特别是对于我来说很奇怪,因为同一个方法在同一个组件中的另一个函数中产生了预期的结果。 –

+0

_anything_发生了什么?控制台中的错误消息? – rossipedia

+0

好吧,现在看起来'render(){... return(...'在保存后(基于控制台记录消息)再次运行,而不是更新后的状态console.log显示'subsections'有新的对象,但是当我检查组件时,它没有State中的新对象 –

this的上下文丢失,你可以尝试回调结合this

var callback = function (newSubsection) { 
     console.log('Has saved id of: ' + newSubsection.id); 
      ..... 
     this.setState({ 
      subsections 
     }); 
    }; 
    callback = callback.bind(this); 
    subsectionTest.save({ 
     name: subsection.name, 
     description: subsection.description 
    }).then(callback), 
    function (error) { 
     console.log('Error:'); 
     console.log(error); 
    }); 

如果您正在使用Babel preset stage 2您可以通过使用箭头功能达致这

 description: subsection.description 
     }).then(newSubsection = > { 
       console.log('Has saved id of: ' + newSubsection.id); 
+0

'.then(newSubsection =()=> {'给出'newSubsection not defined'错误, h让我觉得我没有使用Babel预设阶段2? (对不起,不是先进的开发者,更多的是设计师) –

+0

我认为他的意思是'.then(newSubsection => {' – rossipedia

+0

@rossipedia感谢您纠正:) – StateLess