【问题标题】:TypeError: null is not an object (evaluating 'storagedTechs.split')TypeError:null 不是对象(评估“storagedTechs.split”)
【发布时间】:2021-05-27 01:37:06
【问题描述】:

我收到以下错误:可能未处理的承诺拒绝(id:0:网络请求失败)有时错误 id 会更改为 id:3。有人可以帮帮我吗?

从 'react' 导入 React, {useState, useEffect}; 从 'react-native' 导入 { SafeAreaView, StyleSheet, Image, AsyncStorage };

从 '../components/SpotList' 导入 SpotList;

从'../assets/logo.png'导入标志;

导出默认函数List(){

const [techs, setTechs] = useState ([]);


useEffect(() => {
    AsyncStorage.getItem('techs')
        .then((storagedTechs) => {
            const techsArray = storagedTechs.split(',').map(tech => tech.trim())

            setTechs(techsArray)
        })
},[]);


return (
    <SafeAreaView style={styles.container}>
        <Image style={styles.logo} source={logo}/>

        {techs.map(tech => <SpotList key={tech} tech={tech}/>)}
    </SafeAreaView>
)

}

const 样式 = StyleSheet.create ({ 容器: { 弹性:1,

},
logo: {
    height: 32,
    resizeMode: 'contain',
    alignSelf:'center',
    marginTop: 50
},

})

error in expo go

【问题讨论】:

  • storagedTechs 设置 - 如果它为空,您将收到此错误

标签: javascript react-native expo


【解决方案1】:

我会尝试在 then 块之后添加 catch 块,比如

.then((storagedTechs) => {
    const techsArray = storagedTechs.split(',').map(tech => tech.trim())

    setTechs(techsArray)
})
.catch(err => {
    if (err) console.error(err);
});

这将帮助您进一步调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 2020-11-21
    • 2021-12-29
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多