美文网首页
2017.12.13起初级RN学习总结:

2017.12.13起初级RN学习总结:

作者: MegahertzCHN | 来源:发表于2018-02-09 10:35 被阅读237次

    1.怎样更换主体类:更换文件./App2,选取对应文件的类名LotsOfGreetings

    import { AppRegistry } from 'react-native';
    import {LotsOfGreetings} from './App2';
    AppRegistry.registerComponent('AHelloWorld', () => LotsOfGreetings);
    

    2.nvm管理node版本

    To install or update nvm, you can use the install script using cURL:

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash 
    

    or Wget:

    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
    

    The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
    
    没有理解命令行open .profile
    

    3.Unexpected token (30:33) (null))

    语法有误,请检查当前行的语法
    正确:placeholder = "开始编辑喜欢的内容"
    错误:placeholder : "开始编辑喜欢的内容"
    

    4.Unhandled JS Exception: Stylesheet is not defined

    正确:StyleSheet
    错误:Stylesheet
    

    5.Unhandled JS Exception: "justifyItems" is not a valid style property.

    正确:  
    alternativeLayoutButtonContainer: {
        margin: 20,
        flexDirection: 'row',
        justifyContent: 'space-between'
    }
    错误:
      alternativeLayoutButtonContainer: {
      "margin": 20,
      "flexDirection": "row",
      "justifyItems": "space-between"
    }
    

    6.Unhandled JS Exception: ReferenceError: style is not defined

    正确:<View style={styles.container}>
    错误: <View style={style.container}>
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            justifyContent: 'center',
        },
        buttonContainer: {
            margin: 20
        },
        alternativeLayoutButtonContainer: {
            margin: 20,
            flexDirection: 'row',
            justifyContent: 'space-between'
        }
    })
    

    7._onPresssButton和_onPresssButton() 的区别

    _onPresssButton点击时候调用,_onPresssButton()加载时候调用
    <Button title="Press Me" onPress={this._onPresssButton}/>
    
    箭头函数是什么鬼👻? <Button title="Press Me" onPress={() => this._onPresssButton}/> 和  <Button title="Press Me" onPress={this._onPresssButton}/>作用相同,点击时候调用
    
    

    8.Unhandled JS Exception: ReferenceError: Platform is not defined

    import {
        Image,
        Text,
        View,
        TextInput,
        Button,
        **Platform**,
        Alert,
        StyleSheet,
        TouchableHighlight,
        TouchableOpacity,
        TouchableNativeFeedback,
        TouchableWithoutFeedback,
    } from 'react-native';
    

    9.引入json文件

    const BadgeData = require('./BadgeData.json');
    

    10.不显示图片资源,没有给Image资源大小

    imageStyle: {
       width:80,
       height:80
    },
    

    11.Warning: Each child in an array or iterator should have a unique "key" prop.

    正确:
     <View key={i} style={styles.outViewStyle}>
         <Image source={{uri: badge.icon}} style={styles.imageStyle}/>
    
         <Text style={styles.titleStyle}>
             {badge.title}
         </Text>
     </View>
    
    错误:
     <View style={styles.outViewStyle}>
         <Image source={{uri: badge.icon}} style={styles.imageStyle}/>
    
         <Text style={styles.titleStyle}>
             {badge.title}
         </Text>
     </View>
    

    12.注意:为了使新的图片资源机制正常工作,require中的图片名字必须是一个静态字符串(不能使用变量!因为require是在编译时期执行,而非运行时期执行!)。

    // 正确
    <Image source={require('./my-icon.png')} />
    
    // 错误
    var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
    <Image source={require('./' + icon + '.png')} />
    
    // 正确
    var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png');
    <Image source={icon} />
    

    13.State的使用 ES5&ES6

    ES6:
    // 设置改变和初始值
    constructor(props) {
       super(props);
       this.state = {
           // 当前页码
           currentPage : 0
       }
    }
    // 当一帧滚动结束的时候
    onMomentSrollEndSelf(event) {
       // 1.求出水平方向偏移量
       var offSetX = event.nativeEvent.contentOffset.x;
    
       // 2.求出当前页数 -- 取整
       var currentPage = Math.floor(offSetX / width);
    
       // 3.更新状态机,重绘UI
       this.setState({currentPage:currentPage})
    
    }
    
    ES5:
    // 设置改变和初始值
    getInitialState() {
       return {
           // 当前页码
           currentPage: 0
       }
    
    }
    

    14.Unhandled JS Exception: TypeError: _reactNative.ListView.dataSource is not a constructor

    正确:
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2});
    错误:
    var ds = new ListView.dataSource({rowHasChanged: (r1, r2) => r1 != r2});
    

    15.Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(SyntaxError in /Users/Droi/Desktop/RN/AHelloWorld/App4.js: /Users/Droi/Desktop/RN/AHelloWorld/App4.js: Unexpected token (48:33) (null))

    正确:
    selected={this.state.selectedTabbarItem === 'home'}
    错误:
    selected:{this.state.selectedTabbarItem === 'second'}
    

    16.Unhandled JS Exception: Invariant Violation: React.Children.only expected to receive a single React element child.

    正确:
     {/*首页*/}
     <TabBarIOS.Item
         // icon={require('tabbar_home')}
         // icon={require('../assets/tabbar_home.png')}
         title="首页"
         icon={{uri: 'tabbar_home', scale:3}}
         selected={this.state.selectedItem === 'home'}
         onPress={() => {this.setState({selectedItem: 'home'})}}
     >
         <View></View>
     </TabBarIOS.Item>
    
    错误:
     <TabBarIOS.Item
         // icon={require('tabbar_home')}
         // icon={require('../assets/tabbar_home.png')}
         title="首页"
         icon={{uri: 'tabbar_home', scale:3}}
         selected={this.state.selectedItem === 'home'}
         onPress={() => {this.setState({selectedItem: 'home'})}}
     >
    
     </TabBarIOS.Item>
    

    17.根据平台适配TabBar

    width: Platform.OS === 'ios' ? 30 : 25,
    

    18.Unhandled JS Exception: Invariant Violation: React.Children.only expected to receive a single React element child.

    正确:
     <TabNavigator.Item
         title="首页"
         renderIcon={() => <Image source={require('../img/TabBar/tabbar_home.png')} style={styles.iconStyle}></Image>}
         renderSelectedIcon={() => <Image source={require('../img/TabBar/tabbar_home_highlighted.png')} />}
         selected={this.state.selectedTab === 'home'}
         onPress={() => this.setState({ selectedTab: 'home' })}
     >
         <Home/>
     </TabNavigator.Item>
    
    错误:
     <TabNavigator.Item
         title="首页"
         renderIcon={() => <Image source={require('../img/TabBar/tabbar_home.png')} style={styles.iconStyle}></Image>}
         renderSelectedIcon={() => <Image source={require('../img/TabBar/tabbar_home_highlighted.png')} />}
         selected={this.state.selectedTab === 'home'}
         onPress={() => this.setState({ selectedTab: 'home' })}>
     >
         <Home/>
     </TabNavigator.Item>
    

    语法问题:在onPress行末尾的添加了">",本不应该有的

    19.The <Image> component cannot contain children. If you want to render content on top of the image, consider using aboslute positioning.

    Good catch. I simple PR replacing Image with ImageBackground will fix this.
    <ImageBackground
         ref="backImage"
         source={{uri: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'}}
         style={styles.backImageStyle}
         // resizeMode='cover'
         // resizeMode='contain'
         resizeMode='center' // resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch', 'repeat', 'center']),
     >
         <Text style={styles.textStyle}>设置图片为背景</Text>
     </ImageBackground>
    

    20.组件之间的通信

    this.props.imageDataArr,在自定义组件里面应该起到哪些作用?
    props和state之前又怎样的关系?
    {this.renderRow}和{(rowData) => this.renderRow(rowData)}又怎样的区别?
    

    21.修改WebStorm空格个数 ===> 'detect indent'

    屏幕快照 2018-01-05 上午11.52.27.png

    22.Unhandled JS Exception: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

    正确:
    import CommonCell from './ZHCommonCell';
    
    错误:
    let CommonCell = require('./ZHCommonCell');
    
    export default class ZHCommonCell extends Component {
    }
    

    23.iphone X在RN里面进行适配一些比较蠢的暂时是配方法

    style={{marginTop: ((parseInt(Platform.Version, 10) >= 11 && Platform.OS === 'ios') ? ((width == 375 && height == 812) ? -44 : -20) : 0)}}
    

    相关文章

      网友评论

          本文标题:2017.12.13起初级RN学习总结:

          本文链接:https://www.haomeiwen.com/subject/epwjnxtx.html