React native-React Hook useEffect缺少依赖项:'getAllPost'。包括它或删除依赖项数组。“,

时间:2020-03-24 04:49:31

标签: reactjs react-native react-hooks use-effect

我是React Native的新手,尝试从useEffect调用两个api,但是每次React Hook useEffect has a missing dependency: 'getAllPost'. Either include it or remove the dependency array.都会给我这个错误

这是我的代码

export default function Home({navigation}) {
  const [arrCat, setArrCat] = useState([]);
  const [arrPost, setArrPost] = useState([]);
  const [isLoading, setLoding] = useState(false);

  function getAllCategory() {
    setLoding(true);
    let apiResponse = ApiManager.GET('category/all', [], 'GET');
    apiResponse
      .then(response => {
        let responseJson = response[1];
        let status = response[0];
        setLoding(false);
        let message =
          responseJson.message != null
            ? response.message
            : 'Something went wrong';
        if (status === 200) {
          setArrCat([...responseJson.data]);
          getAllPost();
        }
        setTimeout(function() {
          if (message != null) {
            Toast.showWithGravity(message, Toast.LONG, Toast.BOTTOM);
          }
        }, 120);
      })
      .catch(error => {
        console.error(error);
        Toast.showWithGravity(error, Toast.LONG, Toast.BOTTOM);
        setTimeout(function() {
          setLoding(false);
        }, 60);
      });
  }
  function getAllPost() {
    GetLocation.getCurrentPosition({
      enableHighAccuracy: true,
      timeout: 15000,
    })
      .then(location => {
        console.log(location);
        const dictData = {
          lat: '-37.81400200-33.865143', //location.latitude,
          lang: '144.9546943', //location.longitude,
          record_count: '0',
        };
        console.log(dictData);
        let apiResponse = ApiManager.POST(
          'post/getRecommendedPost',
          dictData,
          'POST',
        );
        apiResponse
          .then(response => {
            let responseJson = response[1];
            let status = response[0];
            if (status === 200) {
              console.log(responseJson);
              setArrPost(oldValue => [...oldValue, ...responseJson.data]);
              console.log(arrPost);
            } else {
              // console.error(responseJson);
              Toast.showWithGravity(
                responseJson.message,
                Toast.LONG,
                Toast.BOTTOM,
              );
            }
          })
          .catch(error => {
            // console.error(error);
            Toast.showWithGravity(error.message, Toast.LONG, Toast.BOTTOM);
            // setTimeout(function() {
            //   setLoding(false);
            // }, 60);
          });
      })
      .catch(error => {
        // const {code, message} = error;
        // console.warn(code, message);
        Toast.showWithGravity(error.message, Toast.LONG, Toast.BOTTOM);
      });
  }

  useEffect(() => {
    console.log('Home screen mounted');
    getAllCategory();
    // getAllPost();
  }, []);

  return ( ....)
}

0 个答案:

没有答案