0

我在 react-native 中遇到了 TabNavigator 和 StackNavigator 的问题。我想在按下按钮时重置导航。我的标签导航中有我的堆栈导航。问题是我只能重置一个导航...

计划:

-tabNavigator
   -listing.js
   -stackNavigator
      -types.js
      -picture.js
      -ad.js

因此,当我在 ad.js 中单击它时,我有一个按钮,我的 stackNavigation 在 Listing.js 上重置为 type.js 和 tabNavigator 所以我想最终返回 Linsting.js。

我的导航:navigation.js

export const resetAction = NavigationActions.reset({
  index: 0,
  actions: [
    NavigationActions.navigate({ routeName: 'Types'}),
  ],
  key: null
})
export const listingAction = NavigationActions.navigate({
    routeName: 'Listing',
  })


const Stack = StackNavigator(
  {
    Types: {screen: SelectTypesScreen},
    Picture: {screen: SelectPictureScreen},
    Ad: {screen: CreateAd}
  })

const Tab = TabNavigator(
  {
    Listing: {screen: ListingScreen},
    Preference: {screen: PreferencesScreen},
    Add: {screen : Stack}
  },
  {

  }
)

export default class App extends Component {
    render() {
      return (
          <Tab />
      );
    }
  }

选项卡导航 linsting.js

export default class Listing extends Component {
    render() {
        return (
            <View>
                <Text> Listing </Text>
            </View>
        );
    }
}

堆栈导航 ad.js

export default class CreateAd extends Component {
    render() {
        return (
            <View>
                <Text style={{marginTop: '80%', marginLeft: '40%'}}> Ad </Text>
                <Button title="Ok" onPress={() => {console.log("displayer 1"), this.props.navigation.dispatch(resetAction), console.log("displayer 2"), this.props.navigation.dispatch(listingAction)}} />
            </View>
        );
    }
}
4

1 回答 1

0

最好更改您的导航嵌套结构。最好按照文档的确切方式进行。根导航器可以是堆栈导航器,您可以将 tabnavigator 作为屏幕添加到它

const MainScreenNavigator = TabNavigator({
  Recent: { screen: RecentChatsScreen },
  All: { screen: AllContactsScreen },
});

const SimpleApp = StackNavigator({
  Home: { screen: MainScreenNavigator },
  Chat: { screen: ChatScreen },
});

参考文档 https://reactnavigation.org/docs/intro/nesting

于 2017-11-10T18:44:27.827 回答