英文:
Property 'navigation' is missing in type '{}' but required in type '{ navigation: any; }' in react native
问题
I have just created a component named as FirstComponent and I want to display this FirstComponent in HomePage component.
code :-
code is here
But it was showing the error Property 'navigation' is missing in type '{}' but required in type '{ navigation: any; }' . Note I am using latest ReactNative which supports tsx.
英文:
I have just created a component named as FirstComponent and I want to display this FirstComponent in HomePage component.
code :-
code is here
But it was showing the error Property 'navigation' is missing in type '{}' but required in type '{ navigation: any; }' . Note I am using latest ReactNative which supports tsx
答案1
得分: 1
目前,您说navigation
属性在调用<FirstComponent />
时是必需的,但您未将navigation
属性传递给它。
因此,解决方案是在调用时添加navigation
属性。
<FirstComponent navigation={navigation} />
但据我看,与其将导航属性传递给FirstComponent
,不如在FirstComponent
中创建一个名为onPress
的prop,它是用户按下FirstComponent
中按钮时要执行的函数。
因此,在重构后,FirstComponent
应该看起来像这样:
import { View, Button } from 'react-native';
function FirstComponent({ onPress }: { onPress: () => void }) {
return (
<View>
<Button title="Login" onPress={onPress} />
</View>
);
}
export default FirstComponent;
而您的HomeComponent
应该看起来像这样:
import { View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import FirstComponent from './components/FirstComponent';
export default function Home({ navigation }: { navigation: any }) {
return (
<View style={styles.container}>
// 请注意,我传递了一个函数到FirstComponent中,
// 而不是传递整个导航对象。
// 通过这种方式,我将导航逻辑提取出来,使其独立于组件之外。
<FirstComponent onPress={() => navigation.navigate('Second_Screen')} />
</View>
);
}
英文:
Currently, you are saying that navigation
prop is required when you call <FirstComponent />
but you are not passing the navigation
prop to it.
So, the solution would be to add navigation
prop while calling it.
<FirstComponent navigation={navigation} />
But according to me, Instead of passing the navigation prop to the FirstComponent
, what you can do is create a prop called onPress
in the FirstComponent
which is a function that has to execute when user presses the button in the FirstComponent
.
So, after refractoring, the FirstComponent
should look something like this
import { View, Button } from 'react-native';
function FirstComponent({ onPress }: { onPress: () => void }) {
return (
<View>
<Button title="Login" onPress={onPress} />
</View>
);
}
export default FirstComponent;
And your HomeComponent
would look something like this
import { View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import FirstComponent from './components/FirstComponent';
export default function Home({ navigation }: { navigation: any }) {
return (
<View style={styles.container}>
// Notice this, I passed a function into FirstComponent
// instead of passing the whole navigation object.
// In this way I have extracted the navigation logic
// out of the component and made it independent.
<FirstComponent onPress={() => navigation.navigate('Second_Screen')} />
</View>
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论