如何在react native paper底部导航中从屏幕导航,在这个例子中,我应该能够从音乐屏幕导航到专辑屏幕,但导航在屏幕中未定义。
零食链接:https://snack.expo.dev/@nitzme/bottom-navigation-example
import * as React from 'react';
import { View, TouchableOpacity } from 'react-native';
import { BottomNavigation, Text } from 'react-native-paper';
const CenterView = ({children}) => { return (
<View style={{ height: '100%', flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center' }}>{children}</View>
)}
const MusicRoute = ({navigation, route}) => {
console.log(navigation, route);
const swichToAlbum = () => {
navigation.navigate('albums');
}
return (
<CenterView>
<Text>Music</Text>
<TouchableOpacity onPress={{swichToAlbum}}>
<Text>Go to Album screen</Text>
</TouchableOpacity>
</CenterView>
)
}
const AlbumsRoute = () => <CenterView><Text>Albums</Text></CenterView>;
const RecentsRoute = () => <CenterView><Text>Recents</Text></CenterView>;
const NotificationsRoute = () => <CenterView><Text>Notifications</Text></CenterView>;
const App = () => {
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'music', title: 'Music', focusedIcon: 'heart', unfocusedIcon: 'heart-outline'},
{ key: 'albums', title: 'Albums', focusedIcon: 'album' },
{ key: 'recents', title: 'Recents', focusedIcon: 'history' },
{ key: 'notifications', title: 'Notifications', focusedIcon: 'bell', unfocusedIcon: 'bell-outline' },
]);
const renderScene = BottomNavigation.SceneMap({
music: MusicRoute,
albums: AlbumsRoute,
recents: RecentsRoute,
notifications: NotificationsRoute,
});
return (
<BottomNavigation
navigationState={{ index, routes }}
onIndexChange={setIndex}
renderScene={renderScene}
shifting={false}
/>
);
};
export default App;
2条答案
按热度按时间hfsqlsce1#
要在单击时导航,您必须创建堆栈导航器。
希望下面的链接能帮助您找到解决方案
https://aboutreact.com/react-native-bottom-navigation/
bqjvbblv2#
因此,实现这一点的一种方法似乎是通过使用上下文使
[index, setIndex]
全局化,然后从任何其他组件更新index
,可以用于在屏幕之间导航。同样的方式
routes array
被传递到路由,所以如果你需要通过路由传递数据,你可以通过将其作为全局上下文来更新路由数组。然后更新索引。还没有找到更好的解决方案。
示例:https://snack.expo.dev/@nitzme/bottom-navigation-example