(react-native-tab-view) 在 TabBar 中使用 navigation.navigate?
(react-native-tab-view) Using navigation.navigate in a TabBar?
我是 React Native 的新手,我正在为我们的一位客户构建应用程序。
我创建了一个显示名称列表的屏幕,这些名称可以按字母顺序显示或按类别分组。我使用 react-native-tab-view
组件来完成这项工作。这两个选项卡中的每一个都呈现各自的屏幕组件,ScreenMapAlpha
(显示 FlatList)和 ScreenMapCategory
(显示 SectionList)。
对于这些列表中的每个 renderItem
道具,我想使用 TouchableOpacity 使每个项目都可以点击进入详细信息页面,但是我无法使用 navigation.navigate() 函数当我不断收到此消息时:
TypeError: undefined is not an object (evaluating '_this3.props.navigation.navigate')
我一直在阅读 react-native-tab-view Github documentation 但我找不到将导航对象传递到两个 Alpha/Category 屏幕组件以使其工作的方法。
我的代码如下所示:
import * as React from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import ScreenMapAlpha from '../screens/map_alpha';
import ScreenMapCategory from '../screens/map_cat';
import { TabView, TabBar, SceneMap } from 'react-native-tab-view';
const initialLayout = { width: Dimensions.get('window').width };
const renderTabBar = props => (
<TabBar
{...props}
indicatorStyle={{ backgroundColor: '#fff' }}
style={{ backgroundColor: '#c00' }}
/>
);
export default function ScreenMap({ navigation }) {
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'first', title: 'Alphabetical' },
{ key: 'second', title: 'Category' }
]);
const renderScene = SceneMap({
first: ScreenMapAlpha,
second: ScreenMapCategory
});
return (
<TabView
navigationState={{index, routes}}
renderScene={renderScene}
onIndexChange={setIndex}
renderTabBar={renderTabBar}
initialLayout={initialLayout}
navigation={navigation}
/>
)
}
map_alpha.js
import React, { useEffect, useState } from 'react';
import { StyleSheet, View, Text, FlatList, Image, TouchableOpacity } from 'react-native';
import TenantListAlpha from '../shared/tableTenantAlpha';
export default function ScreenMapAlpha({ navigation }) {
return (
<View style={styles.container}>
<TenantListAlpha navigation={navigation} />
</View>
)
}
map_cat.js
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import TenantListCat from '../shared/tableTenantCat';
export default function ScreenMapCategory({ navigation }) {
return (
<View style={styles.container}>
<TenantListCat navigation={navigation} />
</View>
)
}
我正在为此应用程序使用 StackNavigator:
mapstack.js
import { createStackNavigator, createTopTabNavigator } from 'react-navigation-stack';
import Map from '../screens/map.js';
import React from 'react';
import CommonHeader from '../shared/header';
import TenantDetail from '../screens/tenant_detail';
const screens = {
Map: {
screen: Map,
navigationOptions: ({ navigation }) => {
return {
headerTitle: () => <CommonHeader navigation={navigation} title="Directory" />
}
}
},
TenantDetail: {
screen: TenantDetail,
navigationOptions: ({ navigation }) => {
return {
//headerTitle: () => <CommonHeader navigation={navigation} title="News" />
headerTitle: () => <Text>Directory</Text>
}
}
}
}
const MapStack = createStackNavigator(screens);
export default MapStack;
我这辈子都想不通 - 如何将导航对象从父屏幕传递到选项卡中的两个屏幕以使 navigation.navigate() 工作?
您似乎并没有真正使用 React 导航。我会实现它,然后将导航道具传递到您所有的屏幕,您还可以使用 useNavigation()
挂钩。
https://github.com/satya164/react-native-tab-view#react-navigation
https://reactnavigation.org/docs/tab-based-navigation
编辑:
我强烈建议实施其中一种反应导航选项卡视图包装器,但是您可以通过正确传递导航道具来使当前代码正常工作。
从 github 页面中您的问题:
https://github.com/satya164/react-native-tab-view#renderscene-required
If you need to pass additional props, use a custom renderScene function:
const renderScene = ({ route }) => {
switch (route.key) {
case 'first':
return <FirstRoute foo={this.props.foo} />;
case 'second':
return <SecondRoute />;
default:
return null;
}
};
在你的情况下看起来像:
const renderScene = ({ route }) => {
switch (route.key) {
case 'first':
return <ScreenMapAlpha navigation={navigation} />;
case 'second':
return <ScreenMapCategory navigation={navigation} />;
default:
return null;
}
};
在 const.js
中创建函数
import React from 'react';
import { useNavigation } from '@react-navigation/native';
export const withNavigation = (Component: any) => {
return (props: any) => {
const navigation = useNavigation();
return <Component navigation={navigation} {...props} />;
};
};
在您的组件中导入和使用
import {
withNavigation,
} from '../../helpers/functions';
export default withNavigation(OrgWiseEvents)
我是 React Native 的新手,我正在为我们的一位客户构建应用程序。
我创建了一个显示名称列表的屏幕,这些名称可以按字母顺序显示或按类别分组。我使用 react-native-tab-view
组件来完成这项工作。这两个选项卡中的每一个都呈现各自的屏幕组件,ScreenMapAlpha
(显示 FlatList)和 ScreenMapCategory
(显示 SectionList)。
对于这些列表中的每个 renderItem
道具,我想使用 TouchableOpacity 使每个项目都可以点击进入详细信息页面,但是我无法使用 navigation.navigate() 函数当我不断收到此消息时:
TypeError: undefined is not an object (evaluating '_this3.props.navigation.navigate')
我一直在阅读 react-native-tab-view Github documentation 但我找不到将导航对象传递到两个 Alpha/Category 屏幕组件以使其工作的方法。
我的代码如下所示:
import * as React from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import ScreenMapAlpha from '../screens/map_alpha';
import ScreenMapCategory from '../screens/map_cat';
import { TabView, TabBar, SceneMap } from 'react-native-tab-view';
const initialLayout = { width: Dimensions.get('window').width };
const renderTabBar = props => (
<TabBar
{...props}
indicatorStyle={{ backgroundColor: '#fff' }}
style={{ backgroundColor: '#c00' }}
/>
);
export default function ScreenMap({ navigation }) {
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'first', title: 'Alphabetical' },
{ key: 'second', title: 'Category' }
]);
const renderScene = SceneMap({
first: ScreenMapAlpha,
second: ScreenMapCategory
});
return (
<TabView
navigationState={{index, routes}}
renderScene={renderScene}
onIndexChange={setIndex}
renderTabBar={renderTabBar}
initialLayout={initialLayout}
navigation={navigation}
/>
)
}
map_alpha.js
import React, { useEffect, useState } from 'react';
import { StyleSheet, View, Text, FlatList, Image, TouchableOpacity } from 'react-native';
import TenantListAlpha from '../shared/tableTenantAlpha';
export default function ScreenMapAlpha({ navigation }) {
return (
<View style={styles.container}>
<TenantListAlpha navigation={navigation} />
</View>
)
}
map_cat.js
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import TenantListCat from '../shared/tableTenantCat';
export default function ScreenMapCategory({ navigation }) {
return (
<View style={styles.container}>
<TenantListCat navigation={navigation} />
</View>
)
}
我正在为此应用程序使用 StackNavigator:
mapstack.js
import { createStackNavigator, createTopTabNavigator } from 'react-navigation-stack';
import Map from '../screens/map.js';
import React from 'react';
import CommonHeader from '../shared/header';
import TenantDetail from '../screens/tenant_detail';
const screens = {
Map: {
screen: Map,
navigationOptions: ({ navigation }) => {
return {
headerTitle: () => <CommonHeader navigation={navigation} title="Directory" />
}
}
},
TenantDetail: {
screen: TenantDetail,
navigationOptions: ({ navigation }) => {
return {
//headerTitle: () => <CommonHeader navigation={navigation} title="News" />
headerTitle: () => <Text>Directory</Text>
}
}
}
}
const MapStack = createStackNavigator(screens);
export default MapStack;
我这辈子都想不通 - 如何将导航对象从父屏幕传递到选项卡中的两个屏幕以使 navigation.navigate() 工作?
您似乎并没有真正使用 React 导航。我会实现它,然后将导航道具传递到您所有的屏幕,您还可以使用 useNavigation()
挂钩。
https://github.com/satya164/react-native-tab-view#react-navigation
https://reactnavigation.org/docs/tab-based-navigation
编辑: 我强烈建议实施其中一种反应导航选项卡视图包装器,但是您可以通过正确传递导航道具来使当前代码正常工作。
从 github 页面中您的问题: https://github.com/satya164/react-native-tab-view#renderscene-required
If you need to pass additional props, use a custom renderScene function:
const renderScene = ({ route }) => {
switch (route.key) {
case 'first':
return <FirstRoute foo={this.props.foo} />;
case 'second':
return <SecondRoute />;
default:
return null;
}
};
在你的情况下看起来像:
const renderScene = ({ route }) => {
switch (route.key) {
case 'first':
return <ScreenMapAlpha navigation={navigation} />;
case 'second':
return <ScreenMapCategory navigation={navigation} />;
default:
return null;
}
};
在 const.js
中创建函数import React from 'react';
import { useNavigation } from '@react-navigation/native';
export const withNavigation = (Component: any) => {
return (props: any) => {
const navigation = useNavigation();
return <Component navigation={navigation} {...props} />;
};
};
在您的组件中导入和使用
import {
withNavigation,
} from '../../helpers/functions';
export default withNavigation(OrgWiseEvents)