在useEffect中使用AsyncStorage返回未定义?

时间:2020-03-31 16:14:23

标签: reactjs react-native asyncstorage use-effect

我有以下内容:const col中传递的得分值未定义。我是React的新手。

    const scores: React.FC<{styles: Styles;}> = ({ styles }) => {
        const [score, setScore] = useState();    
         useEffect(() => {
                        async function retrieveData() {
                            const value = await AsyncStorage.getItem('score');
                            const data = JSON.parse(value);
                            setScore(data.First);
                        }
                        retrieveData();
                    });

const col: columnType[] = getScoreMetrics(status, score);
return (<BC styles={styles} col={col} />);
export default scores;

1 个答案:

答案 0 :(得分:0)

尝试此代码

const scores: React.FC<{styles: Styles;}> = ({ styles }) => {
        const [score, setScore] = useState();    
         useEffect(async() => {
                        async function retrieveData() {
                            const value = await AsyncStorage.getItem('score');
                            const data = await JSON.parse(value);
                            setScore(data.First);
                        }
                        retrieveData();
                    },[]);

const col: columnType[] = getScoreMetrics(status, score);
return (<BC styles={styles} col={col} />);
export default scores;