我不知道为什么动作创建器中的调度功能没有运行。我已经将redux-thunk导入了我的商店,因为它是一个异步函数。
import axios from 'axios'
import { LOG_IN } from './types'
export function signup ({email, password}) {
console.log('signup function ran') // THIS RUNS
return function (dispatch) {
console.log('dispatch function ran') // THIS DOES NOT RUN, NOR DOES ANYTHING BELOW
axios
.post('https://MYAPI.com/signup', {email, password})
.then(response => dispatch({
type: LOG_IN,
payload: response.data.token
}))
.catch(error => {
console.log(error)
})
}
}
答案 0 :(得分:0)
我从来没有弄清楚为什么在函数内返回函数不起作用,但是下面的代码确实起作用了(我将所做的更改加粗了)。如果有人可以让我知道为什么第一个失败的话,我会很喜欢的:)
import axios from 'axios'
import { LOG_IN } from './types'
import store from '../store' // I IMPORTED THE STORE
export function signup ({email, password}) {
console.log('singup function ran')
axios
.post('https://MYAPI/signup', {email, password})
.then(response => store.dispatch({ // I SPECIFICALLY CALLED THE STORE HERE
type: LOG_IN,
payload: response.data.token
}))
.catch(error => {
console.log(error)
})
}