2014-09-18 38 views

回答

10

ReactJS源使用一個名爲__DEV__的變量來跟蹤這個變量,但它沒有被導出,所以它對你的Mixin不可用。但是,其後果是,例如,當你破壞一個不變量時,開發模式ReactJS會給你一個很好的描述錯誤的描述。在生產模式下,它會給出一個通用錯誤,告訴您使用dev版本。

我們可以用它來構建其確定反應的功能在開發模式:

function isDevReact() { 
    try { 
    React.createClass({}); 
    } catch(e) { 
    if (e.message.indexOf('render') >= 0) { 
     return true; // A nice, specific error message 
    } else { 
     return false; // A generic error message 
    } 
    } 
    return false; // should never happen, but play it safe. 
}; 

這工作,因爲不實施render方法例外的是不同的兩種模式:

Development: "Invariant Violation: createClass(...): Class specification must implement a `render` method. Inline JSX script:16" 
Production: "Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings. Inline JSX script:16" 

「渲染」這個詞是特定於我們違反的不變量,所以它只在dev版本的異常中出現。

相關問題