2017-06-15 212 views
1

這段代碼有什麼內在的錯誤嗎?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

回答

3

您的問題是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中的新對象 –

1

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