gpt4 book ai didi

react-native - React Native - StackActions.reset 什么都不做

转载 作者:行者123 更新时间:2023-12-03 23:15:12 25 4
gpt4 key购买 nike

我有这个函数,它在 <TouchableWithoutFeedback> 时被执行区域被按下:

navigateToRentable = () => {
console.log('in navigateToRentable');

this.props
.navigation
.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'Rent',
actions: [
NavigationActions.navigate({
routeName: 'Rentable',
}),
]
}),
],
}))
}

我看到 in navigateToRentable在我的控制台中,所以我知道该方法正在触发,但没有其他任何事情发生 - 控制台中没有其他输出。

这是我的导航结构:

CameraNavigator.js
import React from 'react';
import { StyleSheet, Text, View, Alert, Permissions, Linking, TouchableOpacity, Platform, ImageStore, Dimensions } from 'react-native';
import { createStackNavigator } from 'react-navigation';

import RentScreenNavigator from './RentScreenNavigator';
import RentScreen from '../pages/RentScreen';
import CameraScreen from '../pages/CameraScreen';
import RentableScreen from '../pages/RentableScreen';

export default CameraNavigator = createStackNavigator(
{
OpenCamera: {
screen: CameraScreen,
navigationOptions: ({ navigation }) => ({
header: null
}),
},
RentDetails: {
screen: RentScreen,
navigationOptions: ({ navigation }) => ({
header: null
}),
},
Rentable: {
screen: RentableScreen,
navigationOptions: ({ navigation }) => ({
header: null
}),
}
},
{
initialRouteName: 'Rent',
},
);

CameraScreenNavigator.js
import React from 'react';
import { StyleSheet, Text, View, Alert, Permissions, Linking, TouchableOpacity, Platform, ImageStore, Dimensions } from 'react-native';
import { createStackNavigator } from 'react-navigation';

import CameraNavigator from './CameraNavigator';

export default class CameraScreenNavigator extends React.Component {
constructor(props) {
super(props)
}

render() {
return (
<CameraNavigator/>
)
}
};

应用程序.js
import React from 'react';
import { StyleSheet, Platform, Image, Text, View, ScrollView } from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { Icon } from 'react-native-elements';
import { createBottomTabNavigator } from 'react-navigation';
import firebase from 'react-native-firebase';
import { YellowBox } from 'react-native';

import HomeScreen from './pages/HomeScreen';
import ProfileScreen from './pages/ProfileScreen';
//import CameraScreen from './pages/CameraScreen';
import CameraScreenNavigator from './components/CameraScreenNavigator';
//import RentScreenNavigator from './components/RentScreenNavigator';

YellowBox.ignoreWarnings(['Class RCTCxxModule']);
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);
YellowBox.ignoreWarnings(['You should only render one navigator explicitly in your app, and other navigators should by rendered by including them in that navigator.']);

const AppNavigator = createBottomTabNavigator(
{
Home: HomeScreen,
Profile: ProfileScreen,
Rent: CameraScreenNavigator,
},
{
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
if (routeName === 'Home') {
iconName = 'home';
} else if (routeName === 'Profile') {
iconName = 'user';
} else if (routeName === 'Rent') {
iconName = 'plus-square';
}

// You can return any component that you like here! We usually use an
// icon component from react-native-vector-icons
return (
<Icon
name={iconName}
color={tintColor}
type='feather'
/>
)
}
}),
tabBarOptions: {
activeTintColor: '#fff',
inactiveTintColor: '#c9fffd',
activeBackgroundColor: '#6de3dc',
inactiveBackgroundColor: '#6de3dc'
},
},
);

export default class App extends React.Component {
render() {
return (
<AppNavigator/>
)
}
}

正如您可能从 StackActions.reset 中得知的那样,我正在尝试导航到 Rentable路线是 CameraNavigator 的一部分,这是一个子导航器的主 App航海家。我正在尝试执行 StackActions.reset来自 Home路由,它是根 App 的一部分航海家。

总之,当我按下应该 navigate 的区域时,没有任何 react 。我到 Rentable路线。

更新

我试过了:
this.props
.navigation
.dispatch(
NavigationActions.navigate({
routeName: 'Rent',
actions: [
NavigationActions.navigate({
routeName: 'Rentable',
}),
]
}),
)

第一个 Navigation.navigate有效,但 sub-action导航至 Rentable才不是。

更新

我仍在寻求有关 StackActions.reset 为什么不起作用的答案 - 但对于我想要做的事情(从父导航器导航到子导航器的页面)......我找到了一个解决方法/方法@kashishgrover 在这里: https://github.com/react-navigation/react-navigation/issues/2398

然而,这不是一个完美的解决方法 - 它强制应用程序转到一个中间页面,然后监听一个参数,因此有一瞬间 - 中间页面加载,然后加载预期页面。

最佳答案

this.props.navigation.dispatch(StackActions.reset({
index: 0,
key: null,
actions: [NavigationActions.navigate({ routeName: 'OpenCamera' })],
}));

我需要 index 的正确组合, key , 和 routeNameactions .我也不需要一直导航到 Home页面,只到 Rent页面与主页面相交 App航海家。

关于react-native - React Native - StackActions.reset 什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com