1.問題
解決:以上是因爲項目剛創建的時候沒有執行
執行:react-native start
但是,有的時候會發現執行后里面顯示錯誤 8081端口被佔用,無法使用
ERROR Packager can't listen on port 8081
解決:通過命令
lsof -i:端口號 (例如:lsof -i:8081)
查看所有佔用 8081端口的進程,然後通過以下的命令進行關閉
kill -9 832
之後就可以通過 react-native start 重新執行了
2.問題二
由於在es6中 this 指向的是上層的對象,所以當在方法中進行調用的時候,其實調用的是該方法對象,而無法找到
這個類對象底下的方法,所以會報錯,
解決方案:可以在 constructor 中,對你要調用的方法對象進行綁定 this,如下所示
constructor(props){
super(props);
/*NetUitl.get('http://facebook.github.io/react-native/movies.json',function (ret) {
alert("我收到了嗎")
})*/
const ds=new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
this.state={
dataSource:ds
}
this._renderRow=this._renderRow.bind(this);
}
或是在 render中綁定 this 如下所示
render() {
return (
<View style={{flex:1}}>
<Text style={styles.welcome}>
Welcome!
</Text>
<ListView
showsVerticalScrollIndicator={false}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
/>
</View>
);
}
以上的代碼都是將 this._renderRow 這個對象綁定 外層中的this,所以在方法 _renderRow中就可以直接調用this,不會報錯了