2016-11-21 35 views
0

我試圖在axios調用它不通過之後將值傳遞給URL參數。下面的代碼說明我的問題:在axios中傳遞URL參數與react-redux

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures –

+0

你確定你是這樣激發了內部函數:fetchUser(id)(dispatch,id)? –

回答

2

您已經在fetchUser功能,以及在你的回報功能使用的id

export function fetchUser(id) { 
console.log(id) // works 
return function(dispatch) { 
    console.log(id) // will work 
    ....... 
} 

試着像上面顯示

從你的回報功能刪除,或者如果你使用fetchUser作爲中間件的功能,你可以在fetchUser刪除id作爲PARAM並將其添加爲僅設置了一個param你的return函數