簡體   English   中英

React.js:無法setState

[英]React.js: Unable to setState

通常在React.js中,如果我調用this.setState({data: newData})this.state.data設置為newData並再次呈現整個組件。

在我的代碼中,我有一個搜索欄。 當我按回車鍵時,我想將search_term設置為輸入的關鍵字。

if (evt.keyCode == 13) {
    this.setState({search_term: keyword})
}

關鍵字是“dog”,evt.keyCode在此之前是13(我打印出來)。 我到達了setState調用。 但是,我收到一個錯誤:

Uncaught TypeError: Can't add property framesToPop, object is not extensible.

這導致setState調用失敗,因此,不會設置組件的狀態或重新呈現組件以在頁面上顯示新的搜索詞(假設我在render函數中打印出search_term)。

關於如何調試的任何想法? 可能會發生什么? 如有需要,我可以提供更多信息,請告訴我! :)

[編輯]

這是控制台為framesToPop帶來的代碼

/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/

var invariant = function(condition, format, a, b, c, d, e, f) {
  if ("production" !== "development") {
    if (format === undefined) {
      throw new Error('invariant requires an error message argument');
    }
  }

if (!condition) {
    var error;
    if (format === undefined) {
      error = new Error(
        'Minified exception occurred; use the non-minified dev environment ' +
        'for the full error message and additional helpful warnings.'
      );
    } else {
      var args = [a, b, c, d, e, f];
      var argIndex = 0;
      error = new Error(
        'Invariant Violation: ' +
        format.replace(/%s/g, function() { return args[argIndex++]; })
      );
    }

    error.framesToPop = 1; // we don't care about invariant's own frame
    throw error;
  }
};

我不確定我是否提供了足夠的背景,所以我為此道歉。 不知怎的,我的setState調用拋出了framesToPop錯誤,盡管只調用了一次setState 正如我所說,我在渲染函數中使用div顯示狀態。

<div class='search-display'> {this.state.search_term} </div>

我正在調用一些jquery來編輯在網站上顯示搜索詞的html。

$(.search-display).modifyDiv()

我通過首先將search_term的狀態設置為null來修復它。

this.setState({search_term: null}, function() { 
    this.updateSearchTerm(evt, keyword)
})

this.updateSearchTerm是執行this.setState({search_term, keyword}) 不知道為什么這有效:(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM