admin管理员组文章数量:1405166
In a React Native App, I understand that when you pass your RootNavigator inside createAppContainer and export it, the navigation prop
is implicitly made available in all child ponents.
export default createAppContainer(AppNavigator);
I have this ponent that is supposed to navigate to another screen using the navigation prop. I defined an interface for the props of this ponent like this:
props interface for ComponentA:
interface Props {
count: number;
navigation: NavigationStackProp<{quizId: number}>;
}
Now I render ComponentA like this:
...
render() {
return (
<ComponentA count={0} /> // TypeScript screams here that I am not passing the navigation props
);
}
If I pass the navigation prop with ComponentA like this:
<ComponentA count={0} navigation={props.navigation} />
TypeScript is satisfied.
My question therefore is, do I have to pass navigation prop explicitly like this? It doesn't feel right to me. Is there a way I can suppress TypeScript warnings for this particular situation.
I am new to TypeScript so this might be something trivial but I will appreciate any help.
Thanks
In a React Native App, I understand that when you pass your RootNavigator inside createAppContainer and export it, the navigation prop
is implicitly made available in all child ponents.
export default createAppContainer(AppNavigator);
I have this ponent that is supposed to navigate to another screen using the navigation prop. I defined an interface for the props of this ponent like this:
props interface for ComponentA:
interface Props {
count: number;
navigation: NavigationStackProp<{quizId: number}>;
}
Now I render ComponentA like this:
...
render() {
return (
<ComponentA count={0} /> // TypeScript screams here that I am not passing the navigation props
);
}
If I pass the navigation prop with ComponentA like this:
<ComponentA count={0} navigation={props.navigation} />
TypeScript is satisfied.
My question therefore is, do I have to pass navigation prop explicitly like this? It doesn't feel right to me. Is there a way I can suppress TypeScript warnings for this particular situation.
I am new to TypeScript so this might be something trivial but I will appreciate any help.
Thanks
Share asked Feb 14, 2020 at 11:10 Awa MelvineAwa Melvine 4,0878 gold badges37 silver badges47 bronze badges1 Answer
Reset to default 5You can use optional props by having ?
interface Props {
count: number;
navigation?: NavigationStackProp<{quizId: number}>;
}
本文标签:
版权声明:本文标题:javascript - TypeScript Property 'navigation' is missing in type but required in type 'Props' Re 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744882518a2630291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论