react-native - 在特定路线上按下时使标签导航按钮触发功能
问题描述
目标:当已经在特定路线上时,使用选项卡导航器触发功能。
我有三个选项卡:Inbox
、Camera
和Channel
现在,当我在Camera
路线上时,我想handlePhoto
使用导航器Circle
组件触发我的功能。
我不知道如何传达我正在Camera
前往选项卡导航器的路线这一事实。
任何想法我怎么能做到这一点?
export default function MyTabBar({ state, descriptors, navigation }) {
const cameraRef = createRef();
const handlePhoto = async () => {
if (cameraRef.current) {
let photo = await cameraRef.current.takePictureAsync({
autoFocus: false,
forceUpOrientation: false,
quality: 0,
});
navigation.navigate("Confirm", {
otherParams: photo,
});
}
};
return (
<View
style={{
flexDirection: "row",
backgroundColor: "transparent",
height: 120,
position: "absolute",
left: 0,
right: 0,
bottom: 0,
elevation: 0,
justifyContent: "center",
alignItems: "center",
}}
>
{state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const label =
options.tabBarLabel !== undefined
? options.tabBarLabel
: options.title !== undefined
? options.title
: route.name;
const isFocused = state.index === index;
const onPress = () => {
const event = navigation.emit({
type: "tabPress",
target: route.key,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};
const onLongPress = () => {
navigation.emit({
type: "tabLongPress",
target: route.key,
});
};
return (
<TouchableOpacity
accessibilityRole="button"
accessibilityStates={isFocused ? ["selected"] : []}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
style={{ flex: 1, alignItems: "center" }}
>
{label === "Inbox" && (
<IconContainer>
<NotificationIcon />
<IconText>Inbox</IconText>
</IconContainer>
)}
{label === "Camera" && (
<>
{isFocused ? (
<Circle
handlePhoto={handlePhoto}
style={{
height: 75,
width: 75,
borderWidth: 7,
marginBottom: 80,
}}
/>
) : (
<Circle
handlePhoto={handlePhoto}
style={{ height: 50, width: 50 }}
/>
)}
</>
)}
{label === "Channels" && (
<IconContainer>
<ChatIcon />
<IconText>Channels</IconText>
</IconContainer>
)}
</TouchableOpacity>
);
})}
</View>
);
}
解决方案
推荐阅读
- python - 将单词与其中的其他单词区分开来
- swift - 无法通过 .left 滑动返回原始 ViewController?
- c++ - OpenGL:深度模板缓冲区不会写入附加的底层纹理
- python - SQLAlchemy ORM 子句使用 EXTRACT 和 AT TIME ZONE?
- datastage - datastage scd 阶段正在更新旧记录,而不是插入不匹配的业务密钥
- unit-testing - tSQLt ExpectException 未检测到异常
- prometheus - 如何在 Prometheus 本地磁盘上设置 Thanos 备份历史数据
- javascript - window.onscroll 不适用于视差效果
- amazon-cognito - AWS Cognito:有没有办法在不暴露用户数据的情况下检查用户是否使用 HTTP API 登录?
- java - 如何使用 Eclipse Nebula Grid 在 Eclipse 插件中创建用户输入字段?