react-navigation 监听顶部导航栏点击/滑动状态

更新日期: 2019-03-13阅读: 3.4k标签: react

问题描述:

使用createMaterialTopTabNavigator创建顶部导航栏,希望实现切换到指定的Tab再获取数据,查看官方文档只能找到tabBarOnPress 方法监听点击回调,但是无法监听滑动切换 

 

react.Component{ constructor(props){ super(props); this.tabName= this.props.tabName; //当前tabName this.InitTabName = this.props.InitTabName; //初始化列表 } componentWillMount(){ // 加载初始化Tab列表 if(this.storeName===this.InitTabName){ this.updateList(); } // 监听Tab切换 this.TopBarChangeListener = DeviceEventEmitter.addListener('TabChange',tabName=>{ if(this.tabName===tabName){ //***更新列表*** this.updateList(); } }) } // 更新列表 updateList(){ let {navigation} = this.props; navigation.setParams({hasList:true}); this.loadData(); } loadData(){ //***Send Request*** } componentWillUnmount(){ //移除事件监听器 if(this.TopBarChangeListener){ this.TopBarChangeListener.remove(); } } } render(){ return {/* code... */} } } " title="" data-original-title="复制">
import React from 'react';
import {DeviceEventEmitter,View} from 'react-native';
import { createMaterialTopTabNavigator } from 'react-navigation';

export default class TestPage extends React.Component{
    constructor(props) {
            super(props);
            this.topTabList = ['All','Java', 'Javascript', 'php'];
           
    }
    _getTopBar() {
            let topBars = {}
            this.topTabList.forEach((item, index) => {
                topBars['TopBar_' + item] = {
                    screen: (props)=><ChildComponent {...props} tabName={item} InitTabName={this.topTabList[0]}/>,
                    navigationOptions: {
                        title: item,
                    }
                }
            })
            return topBars
    }
    getTopTabList(){
            if(!this.createTopNavigator){
                this.createTopNavigator = createMaterialTopTabNavigator(
                    this._getTopBar(),
                    {
                          //code...
                    }
                );
            }
            return this.createTopNavigator;
    }
        

    render(){
        const TopTabList = this.getTopTabList();
        // 在导航栏组件render位置使用onNavigationStateChange方法监听顶部导航切换-可监听滑动+点击
        return <View>
            <TopTabList
                onNavigationStateChange={(prevState, currentState)=>{
                     let {index} = currentState;
                     let TabName = currentState.routes[index].routeName;
                     TabName = TabName.split('_')[1];
                    //触发事件监听器,更新列表数据
                    //tip:如果希望切换已经加载过一次之后不重新获取数据,可以通过setParams设一个flag,判断flag是否需要触发监听器
                     DeviceEventEmitter.emit('TabChange',TabName);  
                }}
            />
        </View>
    }
}


class ChildComponent extends React.Component{
    constructor(props){
        super(props);
        this.tabName= this.props.tabName; //当前tabName
        this.InitTabName = this.props.InitTabName; //初始化列表
    }
    componentWillMount(){
         // 加载初始化Tab列表
            if(this.storeName===this.InitTabName){
                this.updateList();
            }
        // 监听Tab切换
            this.TopBarChangeListener = DeviceEventEmitter.addListener('TabChange',tabName=>{
                if(this.tabName===tabName){
                    //***更新列表***
                    this.updateList();
                }
            })
    }
    // 更新列表
    updateList(){
        let {navigation} = this.props;
        navigation.setParams({hasList:true});
        this.loadData(); 
    }
    loadData(){
        //***Send Request***
    }
    componentWillUnmount(){
        //移除事件监听器
        if(this.TopBarChangeListener){
                this.TopBarChangeListener.remove();
            }
        }
    }
    render(){
        return <View>
            {/* code... */}
        </View>
    }
}


链接: https://www.fly63.com/article/detial/2350

如何优雅的设计 React 组件

如今的 Web 前端已被 React、Vue 和 Angular 三分天下,尽管现在的 jQuery 已不再那么流行,但 jQuery 的设计思想还是非常值得致敬和学习的,特别是 jQuery 的插件化。

React深度编程:受控组件与非受控组件

受控组件与非受控组件在官网与国内网上的资料都不多,有些人觉得它可有可不有,也不在意。这恰恰显示React的威力,满足不同规模大小的工程需求。

React框架学习_关于React两种构建应用方式选择

一般在传统模式下,我们构建前端项目很简单。就是下载各种js文件,如JQuery、Echart等,直接放置在html静态文件。Webpack则是JavaScript中比较知名的打包工具。这两个构建工具构成了React应用快速搭建的基础。

Gatsby.js_一款基于React.js静态站点生成工具

Gatsby能快速的使用 React 生态系统来生成静态网站,可以结合React Component、Markdown 和服务端渲染来完成静态网站生成让他更强大。

React创建组件的三种方式及其区别

React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归;具体的三种方式:函数式定义的无状态组件、es5原生方式React.createClass定义的组件、es6形式的extends React.Component定义的组件

react生命周期详解_深入理解React生命周期

React主要思想是通过构建可复用组件来构建用户界面,每个组件都有自己的生命周期,它规定了组件的状态和方法需要在哪个阶段改变和执行。所谓组件就是有限状态机,,表示有限个状态以及在这些状态之间的转移和动作行为的模型。

React + Webpack 构建打包优化

React 相关的优化:使用 babel-react-optimize 对 React 代码进行优化,检查没有使用的库,去除 import 引用,按需打包所用的类库,比如 lodash 、echarts 等.Webpack 构建打包存在的问题两个方面:构建速度慢,打包后的文件体积过大

react router中页面传值的三种方法

这篇文章主要介绍React Router定义路由之后如何传值,有关React和React Router 。react router中页面传值的三种方法:props.params、query、state

react 高阶组件的 理解和应用

react 高阶组件简单的理解是:一个包装了另一个基础组件的组件。高阶组件的两种形式:属性代理(Props Proxy)、反向继承 (Inheritance Inversion)

react中的refs属性的使用方法

React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上。这个特殊的属性允许你引用 render() 返回的相应的支撑实例( backing instance )。这样就可以确保在任何时间总是拿到正确的实例

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!