//Home/index.js
import React, { useState, useEffect } from "react";
import { View, Text, Button } from "react-native";
function HomeScreen({ navigation }) {
const [count, setCount] = useState(0);
useEffect(() => {
navigation.setOptions({
headerRight: () => (
<Button onPress={() => setCount((c) => c + 1)} title="Update" />
),
});
}, [navigation]);
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>{count}</Text>
<Text>Home Screen</Text>
<Button
title="Kullanıcılar"
onPress={() => navigation.navigate("UsersTab")}
/>
{/* <Button title="Users" onPress={() => navigation.push("Users")} /> */}
</View>
);
}
export default HomeScreen;
//Users/index.js
import { View, Text, Button, FlatList } from "react-native";
import React from "react";
import Item from "./item";
const data = [
{
id: 1,
name: "Ahmet",
},
{
id: 2,
name: "Ayşe",
},
{
id: 3,
name: "Fatma",
},
];
const UsersScreen = () => {
return (
<View style={{ flex: 1 }}>
<FlatList
data={data}
keyExtractor={(item) => item.id}
renderItem={({ item }) => <Item item={item} />}
/>
</View>
);
};
export default UsersScreen;
//Users/item.js
import { View, Text, StyleSheet, TouchableOpacity } from "react-native";
import React from "react";
import { useNavigation } from "@react-navigation/native";
const item = ({ item }) => {
const navigation = useNavigation();
return (
<View style={styles.container}>
<TouchableOpacity onPress={() => navigation.navigate("UserDetail", item)}>
<Text style={styles.text}>{item.name}</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
padding: 10,
backgroundColor: "#D8D8D8",
borderBottomWidth: 1,
},
text: {
fontSize: 24,
},
});
export default item;
//UserDetail/index.js
import { View, Text, StyleSheet, Button } from "react-native";
import React from "react";
const UserDetail = ({ route, navigation }) => {
const data = route.params;
return (
<View>
<Text style={styles.text}>UserDetail</Text>
<Text style={styles.text}>{JSON.stringify(data, null, 2)}</Text>
<Button
title="Update the title"
onPress={() => navigation.setOptions({ title: "Updated!" })}
/>
</View>
);
};
const styles = StyleSheet.create({
text: {
fontSize: 24,
},
});
export default UserDetail;
// App.js
import * as React from "react";
import { View, Text, Button } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import Ionicons from "@expo/vector-icons/Ionicons";
// screens
import HomeScreen from "./src/screens/Home";
import UsersScreen from "./src/screens/Users";
import UserDetail from "./src/screens/UsersDetail";
//Home Stack
const HomeStack = createNativeStackNavigator();
function HomeStackScreen() {
return (
<HomeStack.Navigator>
<HomeStack.Screen name="Home" component={HomeScreen} />
</HomeStack.Navigator>
);
}
//Settings Stack
const UsersStack = createNativeStackNavigator();
function USersStackScreen() {
return (
<UsersStack.Navigator>
<UsersStack.Screen name="Users" component={UsersScreen} />
<UsersStack.Screen name="UserDetail" component={UserDetail} />
</UsersStack.Navigator>
);
}
const Tab = createBottomTabNavigator();
function App() {
return (
<NavigationContainer>
<Tab.Navigator
screenOptions={({ route }) => ({
headerShown: false,
tabBarIcon: ({ color, size }) => {
let iconName;
if (route.name === "HomeTab") {
iconName = "home-outline";
} else if (route.name === "UsersTab") {
iconName = "people-outline";
}
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarActiveTintColor: "tomato",
tabBarInactiveTintColor: "gray",
})}
>
<Tab.Screen
name="HomeTab"
component={HomeStackScreen}
options={{ title: "Home" }}
/>
<Tab.Screen
name="UsersTab"
component={USersStackScreen}
options={{ tabBarBadge: 3, title: "Users" }}
/>
</Tab.Navigator>
</NavigationContainer>
);
}
export default App;