在react-native-navigation中,如何在不导航的情况下更新选定的选项卡栏图标?

凯文

基于屏幕上的操作,我想更新所选选项卡的图标(特别是图标上的表示“未读计数”的徽章装饰器),而无需进行导航以导致重新呈现。有哪些方法可以做到这一点?

下面是一个使用问题的基本演示的源代码标签模板expo init,然后修改以证明:https://github.com/kevgrig/rn_navigation_badge_updates

这是该回购的小吃:https : //snack.expo.io/@git/github.com/kevgrig/rn_navigation_badge_updates

关键位是:

  • createBottomTabNavigator导航/ MainTabNavigator.jstabBarIcon该返回S<TabBarIcon />分量并传递一个badgeCountPARAM。

    let homeTabCount = 1;
    HomeStack.navigationOptions = {
      tabBarLabel: 'Home',
      tabBarIcon: ({ focused }) => {
        return (
          <TabBarIcon
            focused={focused}
            name={
              Platform.OS === 'ios'
                ? `ios-information-circle${focused ? '' : '-outline'}`
                : 'md-information-circle'
            }
            badgeCount={homeTabCount}
          />
        );
      },
    };
    
  • TabBarIcon组分组分/ TabBarIcon.js带有可选badgeCountPARAM。

    export default class TabBarIcon extends React.Component {
      render() {
        const { name, focused, badgeCount } = this.props;
    
        return (
          <View style={badgeStyles.badgeView}>
            <Ionicons
              name={name}
              size={26}
              style={{ marginBottom: -3 }}
              color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
            />
            { badgeCount > 0 && (
              <View style={badgeStyles.badgeContainer}>
                <Text style={badgeStyles.badgeText}>{badgeCount}</Text>
              </View>
            )}
          </View>
        );
      }
    }
    
  • 一个Button在主屏幕画面/ HomeScreen.jsonPress处理程序,我想用哪个更新badgeCount主屏幕选项卡。

    export default class HomeScreen extends React.Component {
    
      onPress = () => {
        // TODO - how to update the badgeCount?
      }
    

我调试了的可用方法,props.navigation然后看到了,emit('refocus')但是没有用。

凯文

我用完成了react-redux大概是这样的:

import { createStore, applyMiddleware } from 'redux';

import { connect, Provider } from 'react-redux';

const defaultState = {
  badgeCount: 0,
};

const ACTION_REFRESH_STATE = "ACTION_REFRESH_STATE";

function rootReducer(state = defaultState, action) {
  switch (action.type) {
    case ACTION_REFRESH_STATE:
      const result = Object.assign({}, state);
      if (action.new_state.badgeCount) {
        result.badgeCount = action.new_state.badgeCount;
      }
      return result;
    default:
      return state;
  }
}

function refreshState(new_state) {
  return {
    type: ACTION_REFRESH_STATE,
    new_state: new_state,
  };
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    refreshState: (new_state) => {
      dispatch(refreshState(new_state));
    }
  }
};

const store = createStore(rootReducer);

function routeToIcon(routeName) {
  if (routeName.startsWith("Home")) {
    return { iconName: "md-home", mapStateToProps: state => ({ badgeCount: state.badgeCount }) };
  } [...]
}

[...]
    tabBarIcon: ({ focused, horizontal, tintColor }) => {
      const { iconName, mapStateToProps } = routeToIcon(navigation.state.routeName);
      let IconWithBadgeContainer = connect(mapStateToProps, mapDispatchToProps)(IconWithBadge);
      return <IconWithBadgeContainer name={iconName} size={25} color={tintColor} />;
    },
[...]

export default class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <AppContainer
          ref={navigatorRef => {
            globalNavigatorRef = navigatorRef;
          }}
        />
      </Provider>
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在react-native-router-flux中组合选项卡和导航栏?

如何在React Native中的react选项卡导航中设置默认屏幕Route

如何在React Native Router Flux选项卡中显示图标?

如何在 React Native 中自定义材质底部选项卡导航器?

如何在 React Native 中更改底部选项卡导航器的高度

在React Native中更改React导航选项卡的底部栏容器颜色

使用选项卡栏和导航栏的React Native中的iOS 13黑暗模式

React Native:如何在不导航的情况下刷新前一个屏幕?

我想在React Native的选项卡导航的底部选项卡中添加自定义图标

如何从React Native中的BottomTabNavigator选项卡更新相邻选项卡

在React Native的底部选项卡导航器中更改屏幕时如何更改图标以及文本的颜色?

React Native 矢量图标不会显示在 Android 的底部选项卡导航中

在不属于react native底部选项卡的屏幕中显示导航底部栏

react-native-navigation:如何转到没有选项卡栏的特殊页面?

如何将边框应用于 React Native 中的选定选项卡?

如何在React Navigation 4中显示选项卡栏图标

React-Native-如何在Native Base中动态创建选项卡?

react-native-router-flux选项卡如何更改所选选项卡的图标?

React Native-尝试在React导航中创建带有选项卡导航器的抽屉,而不渲染选项卡的Drawer项

在React Native Navigation中的选项卡之间切换时如何保持堆叠的屏幕

带选项卡导航的react-native抽屉导航

react-native导航v3选项卡图标未显示

React Native 底部选项卡导航器 - 图标不显示

如何在react native和reactnavigation中导航到其他屏幕时删除顶部选项卡导航器

使用 react-native-router-flux 无法正确显示导航栏和选项卡

React Native Navigation 5 在每个堆栈中结合堆栈和选项卡导航器可见性

tabBarOnPress在react-native中的react导航选项卡中不起作用

当我按下 React Native 中的图标 nn 页脚时,我想打开该选项卡

React Native-与平台无关的选项卡导航方法