如何使用redux thunk获取数据

发布于 2025-01-19 16:36:47 字数 2795 浏览 0 评论 0原文

伙计们希望你们都做得很好,我正在使用 redux,但没有得到结果, 我想逐步描述我的问题,希望它有助于理解
(1) 邮递员结果 (i) 获取请求,(ii) JSON 数据

在此处输入图像描述

这是itemList.jsx的代码

const ItemList = (props) => {
    // this value is coming from parent component and here it's value is 1
    //as in postman data 
    const value = props.myData
    console.log(`value is ${value}`)

    const dispatch = useDispatch();

    const { rows } = useSelector(state => state.product);
    console.log(rows)

    useEffect((value) => { // value = 1
        console.log(` hello world ${value}`)

        const jsondata = { "categoryId_fk": 1 } // i am not sure that i would have to pass data like it 
        console.log(`this is my new console ${jsondata}`)
        dispatch(getProductByCategory(jsondata));

    }, [dispatch])

这是productAction.js的代码 //productAction

import axios from 'axios';
import {
    GET_PRODUCTS_BY_CATEGORY_REQUEST,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL,

    CLEAR_ERRORS
} from '../Constrants/ProductConstrants.js'
console.log('hello world')
export const getProductByCategory = (categoryId_fk) => async(dispatch)=>{
    console.log(categoryId_fk)
    try {
        dispatch({type: GET_PRODUCTS_BY_CATEGORY_REQUEST})
        const{ data } = await axios.get('/api/v1/catproduct',categoryId_fk)
        console.log(data)
        dispatch({
            type: GET_PRODUCTS_BY_CATEGORY_SUCCESS,
            payload: data
        })

    } catch (error) {
        dispatch({
            type:GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL,
            payload: error.response.data.message
        })
    }
}

这里是productReducer的代码

import {
    GET_PRODUCTS_BY_CATEGORY_REQUEST,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL,

    CLEAR_ERRORS
} from '../Constrants/ProductConstrants.js'
export const productReducer  = (state = { products : []},action)=>{
    switch(action.type){
        case GET_PRODUCTS_BY_CATEGORY_REQUEST:
            return {
                loading: true,
                products: []
            }
        case GET_PRODUCTS_BY_CATEGORY_SUCCESS:
            return{
                loading: false,
                rows: action.payload.rows,
            }
            case GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL:
                return {
                    loading: false,
                    error: action.payload
                }
                case CLEAR_ERRORS:
            return {
                ...state,
                error: null
            }
            default:
            return state
    }
} 

guys hope you all are doing great, Well I am using redux and I am not getting my result,
I would like to describe my issue step by step I hope it will bater for understanding
(1) POSTMAN RESULT
(i) get request , (ii) JSON data

enter image description here

Here is the code of itemList.jsx

const ItemList = (props) => {
    // this value is coming from parent component and here it's value is 1
    //as in postman data 
    const value = props.myData
    console.log(`value is ${value}`)

    const dispatch = useDispatch();

    const { rows } = useSelector(state => state.product);
    console.log(rows)

    useEffect((value) => { // value = 1
        console.log(` hello world ${value}`)

        const jsondata = { "categoryId_fk": 1 } // i am not sure that i would have to pass data like it 
        console.log(`this is my new console ${jsondata}`)
        dispatch(getProductByCategory(jsondata));

    }, [dispatch])

Here is the code of productAction.js
//product Action

import axios from 'axios';
import {
    GET_PRODUCTS_BY_CATEGORY_REQUEST,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL,

    CLEAR_ERRORS
} from '../Constrants/ProductConstrants.js'
console.log('hello world')
export const getProductByCategory = (categoryId_fk) => async(dispatch)=>{
    console.log(categoryId_fk)
    try {
        dispatch({type: GET_PRODUCTS_BY_CATEGORY_REQUEST})
        const{ data } = await axios.get('/api/v1/catproduct',categoryId_fk)
        console.log(data)
        dispatch({
            type: GET_PRODUCTS_BY_CATEGORY_SUCCESS,
            payload: data
        })

    } catch (error) {
        dispatch({
            type:GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL,
            payload: error.response.data.message
        })
    }
}

here is the code of productReducer

import {
    GET_PRODUCTS_BY_CATEGORY_REQUEST,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS,
    GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL,

    CLEAR_ERRORS
} from '../Constrants/ProductConstrants.js'
export const productReducer  = (state = { products : []},action)=>{
    switch(action.type){
        case GET_PRODUCTS_BY_CATEGORY_REQUEST:
            return {
                loading: true,
                products: []
            }
        case GET_PRODUCTS_BY_CATEGORY_SUCCESS:
            return{
                loading: false,
                rows: action.payload.rows,
            }
            case GET_PRODUCTS_BY_CATEGORY_SUCCESS_FAIL:
                return {
                    loading: false,
                    error: action.payload
                }
                case CLEAR_ERRORS:
            return {
                ...state,
                error: null
            }
            default:
            return state
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2025-01-26 16:36:47

尝试像这样使用 axios

const{ data } = await axios.get(`/api/v1/catproduct/${categoryId_fk}`)

Try to get using axios like this

const{ data } = await axios.get(`/api/v1/catproduct/${categoryId_fk}`)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文