Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 使用空GraphQL数据反应本机钩子_React Native_Graphql_React Hooks - Fatal编程技术网

React native 使用空GraphQL数据反应本机钩子

React native 使用空GraphQL数据反应本机钩子,react-native,graphql,react-hooks,React Native,Graphql,React Hooks,我正在使用react native钩子将数据从主屏幕传递到辅助屏幕,但返回的一些数据将为空 const { profileData } = navigation.state.params; 这会导致如下错误: “未捕获的TypeError:无法读取null的属性'stage'” 这是我的第二个屏幕上的useEffect: useEffect(() => { if(profileData.item.levelrequest.stage == "Submitted") {

我正在使用react native钩子将数据从主屏幕传递到辅助屏幕,但返回的一些数据将为空

const { profileData } = navigation.state.params;
这会导致如下错误: “未捕获的TypeError:无法读取null的属性'stage'”

这是我的第二个屏幕上的useEffect:

 useEffect(() => {
        if(profileData.item.levelrequest.stage == "Submitted") {
            setlrequest05(true)
        } else if(profileData.item.levelrequest.stage == "") {
            setlrequest05(false)
        } else if(profileData.item.levelrequest.stage == "Approved") {
            setlrequest05(false)
        }
    })
我怎么能让它不关心某些用户的stage字段为空

“无法读取null的属性“stage”实际上是告诉您
levelRequest
null
。这当然意味着
stage
的任何一个父级都是
null
…在JS中没有任何神奇的方法可以绕过实际检查对象的每个级别以确保它存在

您可以通过将这些字段设置为必需字段来收紧服务器上的架构,但最简单的处理方法是在函数开头编写一个“guard”条件,如下所示:

useEffect(() => {
  if (!profileData ||
      !profileData.item || 
      !profileData.item.levelRequest ||
      !profileData.item.levelRequest.stage) {
    // do something to handle null data...probably `return` so the other 
    // conditional of the hook isn't fired
  }
  ...rest of your function here...
})
这是一个粗略的例子,但它表达了这个想法。这种情况将在中以更多的专业知识和细节进行讨论。

无法读取null的属性“stage”实际上告诉您
levelRequest
null
。这当然意味着
stage
的任何一个父级都是
null
…在JS中没有任何神奇的方法可以绕过实际检查对象的每个级别以确保它存在

您可以通过将这些字段设置为必需字段来收紧服务器上的架构,但最简单的处理方法是在函数开头编写一个“guard”条件,如下所示:

useEffect(() => {
  if (!profileData ||
      !profileData.item || 
      !profileData.item.levelRequest ||
      !profileData.item.levelRequest.stage) {
    // do something to handle null data...probably `return` so the other 
    // conditional of the hook isn't fired
  }
  ...rest of your function here...
})
这是一个粗略的例子,但它表达了这个想法。本文将以更多的专业知识和细节讨论这种情况