React Native 常用语法(持续更新2021-12-24)

1.安卓打包

cd android && ./gradlew assembleRelease

2.导航返回上一页

this.props.navigator.pop()

3.界面反向传值

 第一个界面 
this.props.navigator.push({
    screen: 'N_Calendars',
    title:'选择日期',
    backButtonTitle: '', // 返回按钮的文字 (可选)
    backButtonHidden: false, // 是否隐藏返回按钮 (可选)
    passProps: {
        identifying:'day',
        callback:this.choosedTime.bind(this)
    }

});
第二个界面
this.props.callback('要传的值');

4.tabbar 设置角标

this.props.navigator.setTabBadge({
    badge:this.state.messageNum // 数字气泡提示, 设置为null会删除
});

5.页面将要加载的时候

constructor(props){
    super(props)
    this.state = {
    };
    this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
}
onNavigatorEvent(event) { // this is the onPress handler for the two buttons together
    // console.log('ApplicationCenterPage event.type', event.type);
    if(event.id==='willAppear'){
        // this.getMessageList();
    }
}

6.通知 DeviceEventEmitter

DeviceEventEmitter.emit('loginSuccess');
// 接收
componentDidMount() {
    this.loginSuccess = DeviceEventEmitter.addListener('loginSuccess', () => {
        this._getBookList();
    })

}
// 接收界面销毁
componentWillUnmount() {
        this.loginSuccess.remove()
}

7.提示框

import Alert from "react-native-alert";
Alert.alert('确定退出', '',
    [
        {text: '取消', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
        {
            text: '确定',
            color:'#729DD5',
            onPress: () => {
                UserInfoStore.removeUserToken(null);
                UserInfoStore.removeUserInfo(null);
                loginJumpSingleton.goToLogin(this.props.navigator);
            },
        },]
    , {cancelable: false}
);
Alert.alert('确定退出', '',
    [{text: '确定', onPress: () => console.log('Cancel Pressed')},]
    , {cancelable: false}
);

8.react-native清除android项目缓存的命令

cd到android目录下执行

./gradlew clean

(./gradlew --stop 停止)

9.react-native 连接android Debug包的命令

adb reverse tcp:8081 tcp:8081

你可能感兴趣的:(React Native 常用语法(持续更新2021-12-24))