我需要做一个动态 Action 。换句话说,它可以重复用于不同的操作。
我试图创建一个加载类型和负载的函数,但出现错误。
我正在尝试让这个功能起作用:
export function getData(url, type) {
const request = Server.get(url)
return (dispatch) =>
request.then((response) => {
dispatch({
type: type,
payload: response.data
})
}).catch(function (error) {
console.log(error)
});
}
但是当我这样调用这个函数时出现错误:
export function getClientes() {
Actions.getData('ClientesEFornecedores', GET_CLIENTES)
}
显示:
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
我是这样调用 getClientes() 函数的:
function ClientesTable(props)
{
const dispatch = useDispatch();
const clientes = useSelector(({erpCliente}) => erpCliente.clientes.data);
useEffect(() => {
dispatch(Actions.getClientes());
}, [dispatch]);
如何使操作可重用?
最佳答案
尝试这样的事情
export const getData=(url, type) =>async dispatch=>{
try{
const response = await Server.get(url);
dispatch({ type: type,payload: response.data })
} catch(err){
console.log(err)
}
}
getClientes 函数
export const getClientes=() => dbActions.getData('ClientesEFornecedores', GET_CLIENTES);
关于javascript - 如何使操作可重用? ReactJS 与 Redux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815383/