使用使用调度来响应钩子调度动作

时间:2019-12-07 13:24:36

标签: reactjs react-hooks

你好,我在使用usedispatch调用动作时遇到问题

他采取了我的行动,但由于某种原因他没有通过我的返回。 在这里停止:console.log('这是获取所有产品'); 而且没有去我的getallproducts:

export const getAllProducts = () => {
    console.log('here is get all products');
    return dispatch => {
        dispatch(isLoadingFetch());
        api.get('/products')
        .then( response => { dispatch(FetchSucess(response.data))})
        .catch( err => { dispatch(FetchFailed(err.message));});
    }
}

我的减速器:

import {FETCH_FAIL, FETCH_SUCESS, FETCH_LOADING} from '../../actions/fetch/actionType';

const initialState = {
    loading: false,
    products: [],
    filteredProducts: [],
    fn:[],
    mw:[],
    ft:[],
    ww:[],
    bs:[],
    stattrek:[],
    normal:[],
    error: null
  };

  export default function productReducer(state = initialState, action) {
    switch (action.type) {
      case FETCH_LOADING:
        return {
          ...state,
          loading: true
        };
      case FETCH_SUCESS:
        return {
          ...state,
          loading: false,
          error: null,
          filteredProducts: action.data.listProducts,
          products: action.data.listProducts,
          fn: action.data.fn,
          mw: action.data.mw,
          ft: action.data.ft,
          ww: action.data.ww,
          bs: action.data.bs,
          stattrek: action.data.listProducts.filter(value=> value.id_types === 1),
          normal: action.data.listProducts.filter(value=> value.id_types === 2)
        };
      case FETCH_FAIL:
        return {
          ...state,
          loading: false,
          error: action.error
        };
      default:
        return state;
    }
  }

我的容器:

import styles from "../assets/cardStyle";
import { useDispatch, useSelector } from "react-redux";
import {useEffect, useCallback} from "react";
import { getAllProducts } from '../store/actions/fetch/index'
const useStyles = makeStyles(styles);

export default function Cards() {

    const classes = useStyles();

    const dispatch = useDispatch();

    const loadProducts= useCallback(async () => {
        dispatch(getAllProducts());
        }, [dispatch]);

        useEffect(() => {
            loadProducts();
            }, [loadProducts]);

    const products = useSelector(state => {
    })

    products.map(product=>(
        <div>
        <Container fixed>
            <Grid container direction="row" >
                <Grid item xs={3}>
                    <Card className={classes.card}>
                        <CardContent className= {classes.cardCarousel}>
                            <Typography variant="body2" color="textSecondary" component="p">
                                This impressive paella is a perfect party dish and a fun meal to cook together with your
                                guests. Add 1 cup of frozen peas along with the mussels, if you like.
                            </Typography>
                        </CardContent>
                    </Card>

                </Grid>
            </Grid>
        </Container>
    </div> 
    ))

    return (
        {products}
    );
}

我无法想象我要去哪里错了 它调用了我的getAllProducts,但没有输入我的redux

2 个答案:

答案 0 :(得分:1)

如果您需要像在getAllProducts()中创建高阶函数一样,返回另一个函数,则需要调用该函数并将dispatch作为第一个参数传递。

const loadProducts= useCallback(() => {
    /**
     * first call returns a function from getAllProducts
     * second will call it and we pass dispatch as the first argument 
     */ 
    getAllProducts()(dispatch);
}, [dispatch, getAllProducts]);    

或者您只需在第一个函数中传递dispatch并立即调用它即可:

export const getAllProducts = (dispatch) => {
    console.log('here is get all products');
    dispatch(isLoadingFetch());
    // fetching data
    api.get('/products')
        .then( response => { dispatch(FetchSucess(response.data))})
        .catch( err => { dispatch(FetchFailed(err.message));});
}
// ...
const loadProducts= useCallback(() => {
    /**
     * first call pass dispatch as the first argument
     */ 
    getAllProducts(dispatch);
}, [dispatch, getAllProducts]);

答案 1 :(得分:1)

您可以继续将useDispatch钩子导入位于../store/actions/fetch/index

的文件中

类似于您在其他文件中所做的方式