Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

javascript - Props is (sometimes only) undefined in React Native. TypeError: Cannot read property 'x' of undefined

I came upon a strange bug today. I am using a component called while loading first in App.js, where it works fine. I am logging props, all fine: an object. Then, navigating to next component, LogInScreen.js using React Navigation, there props is undefined. So, if called from App.js, it works, props is an object, and called from LogInScreen.js it crashes (props is undefined). If I try to destructure it, or call a member props.isNotFullScreen it crashes with the error TypeError: Cannot read property 'isNotFullScreen' of undefined

The only workaround I found was to write const isNotFullScreen = props ? props.isNotFullScreen : false; but still I am interested if one of you has an idea what is happening.

I am using React Native 0.63.3 testing with iOS simulator

Calling from App.js

Calling from LogInScreen.js

logs show props correctly then undefined

Simulator error

Here is my Spinner component

import React from "react";
import { View, ActivityIndicator } from "react-native";
import styles from "./styles/SpinnerStyle";
import { Colors } from "../themes";

const Spinner = (props) => {
  console.log("Spinner props:", props);
  const { isNotFullScreen } = props;
  console.log("isNotFullScreen:", isNotFullScreen);
  return (
    <View style={[styles.spinner, !isNotFullScreen && styles.fullScreen]}>
      <ActivityIndicator
        animating={true}
        size="large"
        color={Colors.baseBlue}
        style={{
          flex: 1,
          justifyContent: "center",
        }}
      />
    </View>
  );
};

export default Spinner;
question from:https://stackoverflow.com/questions/65932508/props-is-sometimes-only-undefined-in-react-native-typeerror-cannot-read-prop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...