【问题标题】:React useEffect raises 'create is not a function' errorReact useEffect 引发“创建不是函数”错误
【发布时间】:2020-12-27 06:21:24
【问题描述】:

我有以下 React Native 模块:

import React, {useContext, useEffect} from 'react';
import {Image, StyleSheet} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import AuthContext from '../context/auth/authContext';

const Intro = () => {
  const getLocalUser = async () => {
    try {
      const value = await AsyncStorage.getItem('user');
      return value;
    } catch (error) {
      console.log(error);
    }
  };
  const localUser = getLocalUser();

  const {setUser} = useContext(AuthContext);
  
  useEffect(() => {
    setUser({localUser});
  }, []);

  return (
    <Image
      style={styles.image}
      source={require('../static/logo_intro.png')}
    />
  );
}

const styles = StyleSheet.create({
  image: {
    width: 430,
    resizeMode: 'center',
    paddingTop: 500
  }
})

export default Intro;

这会引发以下错误:[Tue Sep 08 2020 15:00:47.220] ERROR TypeError: create is not a function. (In 'create()', 'create' is undefined)

我检查了this question,但我已经这样做了。

【问题讨论】:

    标签: react-native use-effect


    【解决方案1】:

    您可能必须使用useState 而不是useEffect

    请参阅https://github.com/facebook/react/issues/15194,了解为什么会以这种方式显示此错误。

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 2022-01-03
      • 2019-04-21
      • 2017-08-16
      • 2019-09-21
      • 2018-02-06
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多