Property 'navigation' is missing in type '{}' but required in type '{ navigation: any; }' in react native

huangapple go评论104阅读模式
英文:

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>
  );
}

huangapple
  • 本文由 发表于 2023年4月6日 20:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定