ReactNative新手入門,常見BUG

cannot add a child that doesn’t have a YogaNode …
佈局文件編寫錯誤,可能是佈局文件中注視導致的.


cannot find variable React
需要導入React. import React, {Component} from ‘react’;


The development server returned response error code: 500

  1. js代碼出現問題,查看node黑框框是否提示錯誤信息,如果有,則是本地js代碼出現錯誤
  2. 設置虛擬機的ip地址,ctrl+M打開開發者菜單,選擇Dev Setting ,選擇Debug server host & port for device ,設置ip地址和端口號 localhost:8081.

You likely forgot to export your component from the file it’s defined in, or you might have mixed up default and named imports.
導包錯誤導致的.需要判斷導入的類是默認導包還是其他導包,默認導包不需要添加{},其他導包需要添加{}


warning: isMounted(…) is deprecated in plain JavaScript React classes.
在app入口文件index.js中使用

import RootStackNavigator from "./pages/navigator/RootStackNavigator";
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);

VirtualizedList: missing keys for items, make sure to specify a key property on each item or provide a custom keyExtractor.
FlatList使用的時候,需要指定keyExtractor屬性

 <FlatList
     ItemSeparatorComponent={() => this.renderItemSeparator()}
     enderItem={info => this._renderItem(info)}
     data={this.state.data}
     keyExtractor={(info) => this._keyExtractor(info)}
  />

 _keyExtractor(item, index) {
        return index+'';
 }

subscriptions[i] is not a function
函數需要綁定this,例如

this.onBackAndroid.bind(this)

_react3.default.createClass is not a function
一些老的框架,使用的是下面的這種方式創建的Component,React最新版本拋棄使用了createClass這個函數,因此會提示錯誤.

var Popover = React.createClass() {
...
}
 module.exports = Popover; 

這時候需要做一些修改,改成下面的方式創建Component

export default class Popover extends Component{
...
}

Possible Unhandled Promise Rejection (id: 0):
提示未處理的promise異常,例如使用await時,如果promise reject一個結果,那麼會提示這個錯誤,並導致後面的代碼得不到執行..這時候可以try…catch一下,或者catch處理一下,例如:

 async testError() {
        return Promise.reject('hello async error....');
 }
 async test() {
     let v2 = await this.testError().catch(err => {
                console.log(err);
      });
      console.log(v2);
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章