RN学习笔记之一些小坑

2017-06-02  本文已影响34人  AnonyPer

1、声明组件时,首字母需要大写:
import TestComponent from './test'; 不能写成import testComponent from './test';,导入时<testComponent>...</testComponent>没有错,使用的时候也不会提示报错,但是运行时会报错
2、ReactNative jcenter()中的react-native架包最新版本为0.20.1,可以查看[https://bintray.com/bintray/jcenter/com.facebook.react:react-native/view](https://bintray.com/bintray/jcenter/com.facebook.react:react-native/view),配置了compile "com.facebook.react:react-native:+"build后从jcenter中只会加载0.20.1的版本。是因为新版的React Native都只在npm里发布。解决方法:

    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$projectDir/../node_modules/react-native/android"
        }
    }
}

如果不行,重启一下AS。

3、一个js文件中只能有一个 export default class TestComponent,这个class 被导入时要使用import TestComponent from ./test,不能带花括号import { TestComponent } from './test' 因为已经默认定义了导出的对象。没有default修饰的import时需要加花括号,而且名字要和对应导出的js文件中写的一样。

4、注意this的使用
在js中,因为可以将方法体作为对象传递,所以如果在方法体中使用的this不代表当前类,而是调用该方法的类。否则会出现很多undefined

上一篇 下一篇

猜你喜欢

热点阅读