javascript TypeError:null不是对象(正在评估“storagedTechs. split”)

mgdq6dx1  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(106)

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

import React, {useState, useEffect} from 'react';
import { SafeAreaView, StyleSheet, Image, AsyncStorage } from 'react-native';

import SpotList from '../components/SpotList';

import logo from '../assets/logo.png';

export default function 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 styles = StyleSheet.create ({
    container: {
        flex: 1,
        
    },
    logo: {
        height: 32,
        resizeMode: 'contain',
        alignSelf:'center',
        marginTop: 50
    },
})

error in expo go

js81xvg6

js81xvg61#

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

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

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

这将有助于您进行进一步的调试。

相关问题