开发者问题收集

TypeError:无法读取未定义的属性(读取‘props’)

2022-05-22
6005

我正在使用 React Native 制作社交网络应用程序。用户可以拍照,单击保存按钮以将图像 uri 作为 prop 传递到保存屏幕。这是代码 -

App.js

const store = createStore(rootReducer, applyMiddleware(thunk))

const Stack = createStackNavigator()

export default function App() {

    const [loading, setLoading] = useState(false)
    const [loggedIn, setLoggedIn] = useState(false)

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    return (
        <Provider store={store}>
            <NavigationContainer>
                <Stack.Navigator initialRouteName="HomeScreen">
                    <Stack.Screen
                        name="HomeScreen"
                        component={HomeScreen}
                        options={{headerShown: false}}
                    />
                    <Stack.Screen
                        name="CreatePostScreen"
                        component={CreatePostScreen}
                        options={{title: "New Post!"}}
                        navigation={this.props.navigation}   //error line
                    />
                    <Stack.Screen
                        name="SaveImageScreen"
                        component={SaveImageScreen}
                        options={{title: ""}}
                    />
                </Stack.Navigator>
            </NavigationContainer>
        </Provider>
    )
}

CreatePostScreen.js

function CreatePostScreen({navigation}) {

    const [hasCameraPermission, setHasCameraPermission] = useState(null)
    const [hasGalleryPermission, setHasGalleryPermission] = useState(null)
    const [type, setType] = useState(Camera.Constants.Type.back)
    const [camera, setCamera] = useState(null)
    const [image, setImage] = useState(null)

    useEffect(() => {
        ~~~~~~~~~~~~~~~
    }, [])

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    const takePicture = async () => {
        if (camera) {
            const data = await camera.takePictureAsync(null)
            setImage(data.uri)
        }
    }    

    return (
        <View style={{flex: 1}}>
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
                <IconButton
                    icon="radiobox-marked"
                    color={Colors.SEA_BLUE}
                    size={35}
                    onPress={() => takePicture()}
                />
                <IconButton
                    icon="check-circle"
                    color={Colors.SEA_BLUE}
                    size={35}
                    onPress={() => navigation.navigate("SaveImageScreen", {image})} //saved image uri is passed to another screen as prop   
                />
            </View>
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        </View>
    )
}

CreatePostScreen 和 SaveImageScreen 都是 App.js 中导航容器下的堆栈屏幕。

但每当我运行应用程序时,我都会收到此错误 ~

TypeError: Cannot read properties of undefined (reading 'props')

或者,有时是这个错误 ~

TypeError: undefined is not an object (evaluating 'this.props')

我应该在代码中做哪些更改?

3个回答

为什么使用 this.props ? 这不是基于类的组件。
您正在使用函数式,您应该编写类似以下内容的内容: export default function App(props) {{
并使用 props.navigation

进行访问
owenizedd
2022-05-22

删除

navigation={this.props.navigation}
AE0011
2022-05-22

您需要删除此

navigation={this.props.navigation}   //error line

关键字用于基于类的组件,并且您的组件是功能组件,并且您不需要传递导航(我知道您想要实现的目标)为了浏览组件,您只需使用来自 react-navigation 的钩子即可,如下所示

import * as React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '@react-navigation/native';

function MyBackButton() {
  const navigation = useNavigation();

  return (
    <Button
      title="Back"
      onPress={() => {
        navigation.goBack();
      }}
    />
  );
}
Mandip Giri
2022-05-23