访问Redux存储时发生无限循环

时间:2020-05-25 16:52:03

标签: react-native react-redux

我正在尝试创建一个应用,并且在当前模块中,我需要从redux商店获取productList的列表。但是,当我尝试从商店中读取整个组件时,便开始了无限循环。如果我评论以下内容,一切都会正常。我是React Native的新手,我不知道自己在做什么错

const vendorProducts = useSelector(store => store.productList.loadedProducts);

import React, {useState, useEffect} from 'react';
import {View, Text, StyleSheet, TouchableOpacity, FlatList} from 'react-native';
import {useDispatch, useSelector} from 'react-redux';
import * as productListAction from '../store/actions/products';
import Constants from '../constants/constants';

const Products = props => {
  console.log('Hello');
  const token = useSelector(store => store.auth.token);
  //const vendorProducts = useSelector(store => store.productList.loadedProducts);
  const dispatch = useDispatch();

  useEffect(() => {
    async function getProductList() {
      let response;
      let productList;
      try {
        const BASEURL = Constants.BASE_URL;
        response = await fetch(BASEURL + 'wp-json/wcfmmp/v1/products/', {
          method: 'GET',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
            Authorization: 'Bearer ' + token,
          },
        });
        productList = await response.json();
      } catch (error) {
        console.error(error);
      }
      dispatch(productListAction.loadedProducts(productList));
    }
    getProductList();
  });
  return (
    <View>
      <Text>Product</Text>
    </View>
  );
};

const style = StyleSheet.create({});

export default Products;

0 个答案:

没有答案