
首先引入插件
yarn add react-native-tab-view
如果用的原生環境要安裝另外幾個插件
yarn add react-native-reanimated react-native-gesture-handler
如果用的expo要安裝
expo install react-native-gesture-handler react-native-reanimated
安裝一個即可
如果你用的6.1以上的版本就直接安裝完了
接着直接在app.js copy如下代碼
import * as React from 'react';
import { View, StyleSheet, Dimensions } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';
const FirstRoute = () => (
<View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);
const SecondRoute = () => (
<View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);
const initialLayout = { width: Dimensions.get('window').width };
export default function TabViewExample() {
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'first', title: 'First' },
{ key: 'second', title: 'Second' },
]);
const renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});
return (
<TabView
navigationState={{ index, routes }}
renderScene={renderScene}
onIndexChange={setIndex}
initialLayout={initialLayout}
/>
);
}
const styles = StyleSheet.create({
scene: {
flex: 1,
},
});
直接就能顯示出tabbar的效果了
貼出官方git鏈接
https://github.com/react-native-community/react-native-tab-view
