在React中使用Redux Toolkit的createEntityAdapter时出错

时间:2020-07-27 04:12:27

标签: reactjs redux redux-toolkit

在使用ReduxToolkit的createEntityAdapter时遇到问题 错误: TypeError:Object(...)不是函数

供应商切片: 从'@ reduxjs / toolkit'导入{createSlice,createAsyncThunk,createEntityAdapter} 从“ axios”导入axios

export const getSupplierData = createAsyncThunk(
    'supplier/getSupplierData', async () => {
        const response = axios.get(
        "http://127.0.0.1:8000/supplier/"
        );
        const data = await (await response).data
        return data
    })

const suppliersAdapter = createEntityAdapter({})


export const { selectAll:selectSuppliers} = suppliersAdapter.getSelectors(
    state=>state.suppliers
)


export const supplierSlice = createSlice({
    name: 'supplier',
    initialState:suppliersAdapter.getInitialState({}),
    reducers: {},
    extraReducers: {
        [getSupplierData.fulfilled]:(state,action)=>action.payload
    }
})


export default supplierSlice.reducer

supplier.js

import { useDispatch } from 'react-redux'
import { getSupplierData,selectSuppliers} from './SupplierSlice'
const dispatch = useDispatch()
const classes = useStyles();
useEffect(() => {
   dispatch(getSupplierData()) 
}, [])
const suppliers = useSelector(selectSuppliers)
console.log(suppliers)

1 个答案:

答案 0 :(得分:0)

如下更改代码:

export const getSupplierData = createAsyncThunk(
    'supplier/getSupplierData', async () => {
        const response = await axios.get(
        "http://127.0.0.1:8000/supplier/"
        );
        const data = await response.data;
        return data
});