typescript - 如何为反应导航器创建包装子组件
问题描述
Tab.Navigator 组件只能有 Tab.Screen 组件作为直接子组件。
在 Typescript 中,有没有办法将 Tab.Screen Type 转换为 TabButton 函数?
const App = () => {
return (
<NavigationContainer>
<Tab.Navigator tabBarOptions={{
...>
<Tab.Screen name={'name1'} component={component1} />
<Tab.Screen name={'Add Photos'} component={FeedScreen}
options={{
tabBarButton: ...
/>
<TabButton title={'Setting'} component={SettingScreen} imageSrc={'./icons/accountDark.png'}/>
}
这是我想要做的
type TabButtonProps = {
title: string,
component: any,
imageSrc: string,
}
const TabButton = ({title, component, imageSrc}: TabButtonProps) => {
return (
<Tab.Screen name={title} component={component} options={{
tabBarIcon: ({focused}) => (
<View style={{alignItems: 'center', justifyContent: 'center', top: 10}}>
<Image source={imageSrc as ImageSourcePropType}
resizeMode='contain'
style={{width: 25, height: 25, tintColor: focused ? '#999999' : '#dddddd'}}
/>
</View>
)}}/>
)
}
我有什么:
Error: A navigator can only contain 'Screen' components as its direct children (found 'TabButton'). To render this component in the navigator, pass it in the 'component' prop to 'Screen'.
解决方案
这与 TypeScript 无关,因此强制转换无法解决此问题。您可以使用组件生成而不是包装组件:
const createTabButton = ({title, component, imageSrc}: TabButtonProps) => {
return (
<Tab.Screen name={title} component={component} options={{
tabBarIcon: ({focused}) => (
<View style={{alignItems: 'center', justifyContent: 'center', top: 10}}>
<Image source={imageSrc as ImageSourcePropType}
resizeMode='contain'
style={{width: 25, height: 25, tintColor: focused ? '#999999' : '#dddddd'}}
/>
</View>
)}}/>
)
}
...
const App = () => {
return (
<NavigationContainer>
<Tab.Navigator tabBarOptions={{
...>
<Tab.Screen name={'name1'} component={component1} />
<Tab.Screen name={'Add Photos'} component={FeedScreen}
options={{
tabBarButton: ...
/>
{createTabButton({title: 'Setting', component: SettingScreen, imageSrc:'./icons/accountDark.png'})}
<NavigationContainer/>
}
您还可以通过设置显示名称来“欺骗”库:
TabButton.displayName = 'Tab.Screen'
推荐阅读
- php - 我如何通过 php 中的三维数组为 Rubix Cube 着色
- python - 在循环中绘制子图
- javascript - 正则表达式使用 match() 在 javascript 中提取字符串数组
- javascript - 反应本机函数返回未定义
- angular - 如何将 Angular ngFor 与 Observable 一起使用
- spring - Spring 引起:org.springframework.core.serializer.support.SerializationFailedException:无法反序列化有效负载?集群模式
- javascript - 如何使它在我选择错误答案时弹出“重试”按钮
- git - 无法将我的 git 存储库连接到 Jenkins
- architecture - 微服务架构 - 每个服务是否需要 API
- javascript - 如何在 React 项目中正确使用/导入 JavaScript 文件?