我试图在axios调用之后将值传递给URL参数。下面的代码解释了我的问题:
export function fetchUser(id) {
console.log(id) // works
return function(dispatch, id) {
console.log(id) // does not work
axios.get("https://jsonplaceholder.typicode.com/users?id=" + id)
.then((response) => {
dispatch({type: "FETCH_USER_FULFILLED", payload: response.data})
})
.catch((err) => {
dispatch({type: "FETCH_USER_REJECTED", payload: err})
})
}
}
答案 0 :(得分:2)
您在id
函数以及返回函数中使用了fetchUser
。
export function fetchUser(id) {
console.log(id) // works
return function(dispatch) {
console.log(id) // will work
.......
}
尝试将其从返回函数中删除,如上所示
或者,如果您使用fetchUser作为中间件功能,则可以将id
删除为fetchUser
中的参数,并将其作为参数添加到return
函数