首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应-在儿童屏幕上的本地导航?

反应-在儿童屏幕上的本地导航?
EN

Stack Overflow用户
提问于 2018-10-29 18:10:59
回答 1查看 1.1K关注 0票数 0

如何在子屏幕中使用导航?

我在App.js中创建导航。然后为每个屏幕创建堆栈导航器(在本例中为FindGroupScreen)。在FindGroupScreen.js中,我创建了一个子屏幕(TravelListDetail),我想在其中使用导航。在FindGroupScreen中,我通常只使用

代码语言:javascript
复制
this.props.navigation.navigate('Chat');

导航到另一个屏幕。但这在子屏幕(TravelListDetail)中不起作用。我应该怎么做才能使导航在子屏幕上工作?

App.js:

代码语言:javascript
复制
imports ...

const FindGroupStack = createStackNavigator({
    FindGroup: FindGroupScreen,
},
{ headerMode: 'none', }
);

// ...stacks

const MainBottomTab = createBottomTabNavigator(
{
    Home: HomeStack,
    FindGroup: FindGroupStack,
    Trip: TripStack,
    Chat: ChatStack,
    Menu: MenuStack,
},
{
navigationOptions: ({ navigation }) => ({
  tabBarIcon: ({ focused, tintColor }) => {
    const { routeName } = navigation.state;
    let iconName;
    if (routeName === 'Home') {
      //iconName = `facebook${focused ? '' : '-outline'}`;
      iconName = `home`;
    } else if (routeName === 'FindGroup') {
      iconName = `map-marked-alt`;
    } else if (routeName === 'Trip') {
      iconName = `map-marker-alt`;
    } else if (routeName === 'Chat') {
      iconName = `comments`;
    } else if (routeName === 'Menu') {
      iconName = `bars`;
    }
    //return <Ionicons name={iconName} size={25} color={tintColor} />;
    return <Icon name={iconName} size={20} color={tintColor} />;
  },
}),
tabBarOptions: {
  activeTintColor: '#f0ca6d',
  inactiveTintColor: '#ffffff',
  labelStyle: {
    fontSize: 12,
  },
  style: {
    backgroundColor: '#303030',
  },
},
}
);

export default createSwitchNavigator(
{
    AuthLoading: AuthLoadingScreen,
    App: MainBottomTab,
    Auth: AuthStack,
},
{
    initialRouteName: 'AuthLoading',
}
);

FindGroupScreen:

代码语言:javascript
复制
imports ...
import TravelListDetail from '../Detail/TravelListDetail';

class FindGroupScreen extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      selectedItem: null,
      phase: 'phase-0',
    };
  }

  renderPage() {
    const { selectedItem, position, detailItem, phase } = this.state;

    return (
      <View style={{ flex: 1, backgroundColor: '#606060' }}>
        <List
          selectedItem={selectedItem}
          onItemPress={this.onItemPressed}
          phase={phase}
        />
        <TravelListDetail
          phase={phase}
          selectedItem={selectedItem}
          onBackPress={this.onBackPressed}
          onSharedElementMovedToDestination={
            this.onSharedElementMovedToDestination
          }
          onSharedElementMovedToSource={this.onSharedElementMovedToSource}
        />
      </View>
    );
  }
  render() {
    const {
      phase,
    } = this.state;

    return (
      <SharedElementRenderer>
        <View style={styles.container}>
          <ToolbarBackground
            isHidden={phase !== 'phase-1' && phase !== 'phase-2'}
          />
          {this.renderPage()}
        </View>
      </SharedElementRenderer>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

export default FindGroupScreen;

TravelListDetail:

代码语言:javascript
复制
      <View style={styles2.viewCenter}>
        <TouchableOpacity
            style={styles2.buttonStyle}
            activeOpacity = { .5 }
            onPress={ this.gotoChatScreen }
        >
          <Text style={styles2.buttonTextStyle}> Share Travel </Text>
        </TouchableOpacity>
      </View>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-29 18:14:53

把它当作道具传给孩子的屏幕。

代码语言:javascript
复制
<TravelListDetail
          navigation={this.props.navigation}
          phase={phase}
          selectedItem={selectedItem}
          onBackPress={this.onBackPressed}
          onSharedElementMovedToDestination={
            this.onSharedElementMovedToDestination
          }
          onSharedElementMovedToSource={this.onSharedElementMovedToSource}
        />

然后,在孩子身上,你写:

代码语言:javascript
复制
props.navigation.navigate('Welcome')

(如果是类组件,则使用this.前缀)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53051444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档