[英]return (dispatch) do nothing
i have a component which run this我有一个运行它的组件
import { handleAddQuestion } from '../actions/questions';
handleAddQuestion(optionOneText, optionTwoText, author)
in actions/questions
i have在我的
actions/questions
中
import { _saveQuestion } from '../utils/_DATA';
export function handleAddQuestion (optionOneText, optionTwoText, authedUser) {
console.log("before")
return (dispatch) => {
console.log("after")
_saveQuestion({optionOneText, optionTwoText, author: authedUser}).then((question) => {
let qid = question.id
dispatch(saveUserQuestion(authedUser, qid))
dispatch(addQuestion(question))
})
}
}
i get before
in console but not after
no action is triggered, nothing happens at all no matter what i change,, it's like i cannot do this return (dispatch)
,!我在控制台
before
,但在没有触发任何操作after
没有,无论我改变什么,什么都没有发生,就像我不能做这个return (dispatch)
,! i tried the same thing in another place which worked perfectlly, logged the data and triggered the actions !我在另一个地方尝试了同样的事情,效果很好,记录了数据并触发了动作! what is wrong here !!!!!!!
这里有什么问题!!!!!!!
more info _DATA_.js
更多信息
_DATA_.js
export function _saveQuestion (question) {
return new Promise((res, rej) => {
const authedUser = question.author;
const formattedQuestion = formatQuestion(question);
setTimeout(() => {
questions = {
...questions,
[formattedQuestion.id]: formattedQuestion
}
users = {
...users,
[authedUser]: {
...users[authedUser],
questions: users[authedUser].questions.concat([formattedQuestion.id])
}
}
res(formattedQuestion)
}, 1000)
})
}
reducers/questions.js
export function questions(state = {}, action) {
switch (action.type) {
case ADD_QUESTION:
const { question } = action;
return {
...state,
[question.id]: question,
};
///
any help?有什么帮助吗?
You are returning what is called a thunk
here - and thunks only get executed when actually being dispatched.您在这里返回了所谓的
thunk
- thunk 只有在实际调度时才会执行。 Compare that with how it is called in the other places.将其与其他地方的调用方式进行比较。
So correct would be calling所以正确的是打电话
dispatch(handleAddQuestion(optionOneText, optionTwoText, author))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.