【问题标题】:using of Custom Tabs with StackNavigator?在 StackNavigator 中使用自定义选项卡?
【发布时间】:2017-12-22 00:55:17
【问题描述】:

我有用户列表,每个用户在列表中都有自己的显示图像。

我正在尝试的是,每当用户按下显示图像时,通过 stackNavigation 重定向到他/她的个人资料。

自定义标签视图:

    const CustomTabView = ({ router, navigation }) => {
  const { routes, index } = navigation.state;

  const ActiveScreen = router.getComponentForState(navigation.state);

  const routeNav = addNavigationHelpers({
    ...navigation,
    state: routes[index],
  });
  const routeOptions = router.getScreenOptions(routeNav, 'tabBar');
  return (
    <View style={styles.container}>
      <CustomTabBar
        navigation={navigation}
        activeRouteName={routes[index].routeName}
        icon={routeOptions.tabBarIcon}
      />
      <ActiveScreen
        navigation={addNavigationHelpers({
          ...navigation,
          state: routes[index]
        })}
      />
    </View>
  );
}; 

StackNavigator: // goToProfile.js // 也尝试放入 index.anndroid.js 但没有找到导出方法

const goToProfile = StackNavigator({
  Profile: {
    screen: Profile,
    navigationOptions: ({ navigation }) => ({
      title: `${navigation.state.params.person.name.first} ${navigation.state.params.person.name.last}`
    })
  },
})

自定义标签: //index.android.js

const CustomTabRouter = TabRouter(
  {
    Chat: {
      screen: Chats,
      path: ""
    },
    Status: {
      screen: Contacts,
      path: "notifications"
    },
    Camera: {
      screen: Camera,
      path: "settings"
    }
  },
  {
    initialRouteName: "Chat",
  },


     );

    const CustomTabs = createNavigationContainer(
     createNavigator(CustomTabRouter)(CustomTabView)
    );

也是我的组件:

          <TouchableOpacity
            onPress = { () =>  this.props.navigation.navigate('Profile', { item } ) }  >
              <Avatar
                source={{ uri: item.picture.thumbnail }}
              />
          </TouchableOpacity>

【问题讨论】:

  • 有什么问题?
  • @Cruiser 如何在我的聊天屏幕中使用 stacknavigator 来实现这一目标
  • SO 不是代码编写服务。我建议您尝试一下,并在遇到问题、遇到错误或遇到其他问题时提出问题
  • @Cruiser 哈哈,我知道,我尝试了不同的方法,没有成功
  • @Cruiser 我尝试使用 stacknavigator const 在聊天屏幕中包含chats { screen: goToProfile }

标签: reactjs react-native react-native-android react-navigation


【解决方案1】:

您的 Stack Navigator 将如下所示:

    const ChatStack= StackNavigator({
      Chat:{screen: Chat,
      navigationOptions: {
       header: null,
     }},
     Profile: {
        screen: Profile,
        navigationOptions: ({ navigation }) => ({
          title: `${navigation.state.params.person.name.first} ${navigation.state.params.person.name.last}`,
          tabBarVisible: false
         // depending if you want to hide the tabBar on the profile page if not remove it.
        })
      },
    }) 

那么您的 customTab 将采用以下形状:

const CustomTabRouter = TabRouter(
  {
    Chat: {
      screen: ChatStack,
    },
    Status: {
      screen: Contacts,
      path: "notifications"
    },
    Camera: {
      screen: Camera,
      path: "settings"
    }
  },
.........

通过这些更改,您应该能够使用您的

导航到个人资料屏幕
this.props.navigation.navigate('Profile', { item } )

【讨论】:

    【解决方案2】:

    您想要调度导航操作,而不是渲染新堆栈 - 虽然我不确定您的导航器是否正确构建以使其成功...

    _goToProfile = (person) => {
      <goToProfile navigate={this.props.navigation} />
    }
    

    应该是

    _goToProfile = (person) => {
      this.props.navigation.navigate('Profile', person)
    }
    

    【讨论】:

    • 已经试过了...屏幕配置文件没有在顶级组件中定义,所以它不起作用
    • 正确,您的示例代码没有包含足够的信息,我无法提供完整的答案。查看所有导航器会很方便。如果您将screen: Profile, 更改为screen: CustomTabs 是否有效?
    • 不,这不起作用...CustomTabs不被视为屏幕:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多