簡(jiǎn)年1: React-Native踩坑記(匯總一)

剛開(kāi)始學(xué)React-Native , 無(wú)法使用Import


import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  Image,
  View
} from 'react-native';

export default class Logo extends Component{
    render(){
        let pic = {
            uri: 'https://upload.wikimedia.org/wikipedia/commons/d/de/Bananavarieties.jpg'
        }
        return (
            <View style={logo_styles.container}>
                <Image source={pic} style={{width: 193, height: 110}} />
            </View>
            );
    }
}

const logo_styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  
});

在index.android.js中,寫(xiě)

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  TextInput,
  Image,
  View
} from 'react-native';

import Logo from "Logo"


export default class zz extends Component {
  render(){
        return <Logo />;
    //return <Welcome2 name="HHHHH" />
  }

}

AppRegistry.registerComponent('zz', () => zz);

但如下錯(cuò)誤

Screenshot_20170122-095619.png

經(jīng)過(guò)了多方查找,發(fā)現(xiàn)錯(cuò)誤在這里

import Logo from "./Logo"

這對(duì)習(xí)慣了當(dāng)前路徑就在編譯路徑下的人。。就是個(gè)坑。。。

好吧,我菜!!!

Screenshot_20170122-100150.png

this關(guān)鍵字

先看報(bào)錯(cuò)的代碼

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  TextInput,
  Image,
  View
} from 'react-native';

import MyScene from "./MyScene"
import Logo from "./node_a/Logo"
import Home from "./node_a/Home"

export default class BaZi extends Component {
  constructor(props){
      super(props);
      this.state = {isLogo: true};
  }
  jumpToHome(){
      console.log("jumptohome :");
      this.setState({isLogo:false});
  }
  render() {
    console.log("render :"+JSON.stringify(this.state));
      if (this.state.isLogo) {
        return <Logo onTimeOut={this.jumpToHome}/>;
      }else{
        return <Home />;
      }
  }

}

AppRegistry.registerComponent('bazi', () => BaZi);

這段代碼的作用比較容易明白。
先顯示一個(gè)Logo頁(yè)面,Logo里使用setTimeout觸發(fā)一個(gè)onTimeOut事件。
(見(jiàn)我的 第一個(gè)坑的文章 http://www.lxweimin.com/p/f688377e6a6a
onTimeOut調(diào)用本地的jumpToHome
JumpToHome修改state,使得顯示一個(gè)Home出來(lái)。

但這個(gè)看起來(lái)沒(méi)問(wèn)題的代碼,報(bào)錯(cuò)了。

錯(cuò)誤解決

Screenshot_20170122-112133.png

使用console.log(this)打印出來(lái)發(fā)現(xiàn)是空的。。。。

我似乎明白點(diǎn)什么了。。。網(wǎng)上找了個(gè)代碼

this.jumpToHome = this.jumpToHome.bind(this);

放到constructor中。

...我真想吐個(gè)槽。。用一個(gè)this真的有必要綁一下嗎???!!!!

另一種寫(xiě)法

當(dāng)然我對(duì)上述代碼又做了點(diǎn)優(yōu)化

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  TextInput,
  Image,
  View
} from 'react-native';

import MyScene from "./MyScene"
import Logo from "./node_a/Logo"
import Home from "./node_a/Home"

export default class BaZi extends Component {
  constructor(props){
      super(props);
      this.state = {isLogo: true};
  }
  render() {
      if (this.state.isLogo) {
        return <Logo onTimeOut={()=>{this.setState({isLogo:false});}}/>;
      }else{
        return <Home />;
      }
  }

}

AppRegistry.registerComponent('bazi', () => BaZi);

用lambda表達(dá)式的寫(xiě)法,不用綁定!!!

有一種寫(xiě)法

<Logo onTimeOut={()=>{this.jumpToHome.bind(this)}/>;

這種寫(xiě)法似乎比較常見(jiàn)!
比如

render() {
    return (
        <View style={{flex: 1, padding: 20}}>
            <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
                <Text style={styles.button}>Push Plain Screen</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={ this.onPushStyledPress.bind(this) }>
                <Text style={styles.button}>Push Styled Screen</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={ this.onModalPress.bind(this) }>
                <Text style={styles.button}>Show Modal Screen</Text>
            </TouchableOpacity>

            {
                Platform.OS === 'ios' ?
                    <TouchableOpacity onPress={ this.onLightBoxPress.bind(this) }>
                        <Text style={styles.button}>Show LightBox</Text>
                    </TouchableOpacity> : false
            }

            {
                Platform.OS === 'ios' ?
                    <TouchableOpacity onPress={ this.onInAppNotificationPress.bind(this) }>
                        <Text style={styles.button}>Show In-App Notification</Text>
                    </TouchableOpacity> : false
            }

            <TouchableOpacity onPress={ this.onStartSingleScreenApp.bind(this) }>
                <Text style={styles.button}>Show Single Screen App</Text>
            </TouchableOpacity>
        </View>
    );
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,552評(píng)論 25 708
  • 先看報(bào)錯(cuò)的代碼 這段代碼的作用比較容易明白。先顯示一個(gè)Logo頁(yè)面,Logo里使用setTimeout觸發(fā)一個(gè)on...
    taiji1985閱讀 11,212評(píng)論 4 4
  • 我從崖邊跌落, 落入星空遼闊, 銀河不清不濁, 不知何以擺脫, 我從崖邊跌落, 落入?yún)采饺f(wàn)座, 呼聲不烈不弱, 夢(mèng)...
    銀河不清不濁閱讀 204評(píng)論 0 0
  • 這個(gè)世界 沒(méi)有一處安住的地方 連腳下的土地 也可能隨時(shí)會(huì)背叛你 是活在空氣中 當(dāng)發(fā)現(xiàn)自己變成了他人的縮影時(shí) 又發(fā)現(xiàn)...
    怡馨宅閱讀 372評(píng)論 6 8
  • 導(dǎo)師約稿 文 |張政鷹 01 “女生發(fā)什么樣的朋友圈會(huì)讓你忍不住點(diǎn)贊?”“有趣的。” 和一個(gè)男性朋友吃飯的時(shí)候,他...
    冷愛(ài)閱讀 24,247評(píng)論 0 5