Redux Thunk 未触发 return 语句
Redux Thunk not firing return statement
我在 Stackover 流程中看到过类似的问题,但其中 none 似乎解决了我的问题。我正在尝试使用 redux thunk 发送一个动作,但不幸的是,我的 thunk 函数没有触发 return 语句。
我试过像往常一样使用 redux thunk:
export function handleLikeThunk(id, likeType){
console.log('thunk1')
return (dispatch) => {
console.log('thunk2', id);
}
}
但是代码只到达 'thunk1' 消息。它从不显示 'thunk2'
我正在尝试处理一个赞按钮,所以首先我导入了 redux thunk 函数:
import { handleLikeThunk } from '../redux/actions/posts';
我有一个函数,只要按下点赞按钮就会调用 thunk:
handleLike = (e) => {
const { likes, id } = this.state
if(e.target.getAttribute('name') === 'upVote'){
this.setState((perviouState)=>({
likes: perviouState.likes + 1
}));
handleLikeThunk(id, 'upVote');
}
if(e.target.getAttribute('name') === 'downVote' && likes > 0){
this.setState((perviouState)=>({
likes: perviouState.likes - 1
}));
handleLikeThunk(id, 'downVote');
}
}
为此,我将我的组件连接到商店
const mapDispatchToProps = (dispatch) => {
return {
handleLikeThunk: (id, likeType) => dispatch(handleLikeThunk(id,
likeType))
}
}
export default connect(null, mapDispatchToProps)(Post)
就像我说的,我希望能够在后端使用 thunk 函数 post 此信息,但它永远不会进入 thunk 函数内的 return statament。
看起来您正在调用原始函数,而不是您放入 mapDispatchToProps
的函数,请尝试将 handleLikeThunk(id, 'upVote');
更改为 this.props.handleLikeThunk(id, 'upVote');
我在 Stackover 流程中看到过类似的问题,但其中 none 似乎解决了我的问题。我正在尝试使用 redux thunk 发送一个动作,但不幸的是,我的 thunk 函数没有触发 return 语句。
我试过像往常一样使用 redux thunk:
export function handleLikeThunk(id, likeType){
console.log('thunk1')
return (dispatch) => {
console.log('thunk2', id);
}
}
但是代码只到达 'thunk1' 消息。它从不显示 'thunk2'
我正在尝试处理一个赞按钮,所以首先我导入了 redux thunk 函数:
import { handleLikeThunk } from '../redux/actions/posts';
我有一个函数,只要按下点赞按钮就会调用 thunk:
handleLike = (e) => {
const { likes, id } = this.state
if(e.target.getAttribute('name') === 'upVote'){
this.setState((perviouState)=>({
likes: perviouState.likes + 1
}));
handleLikeThunk(id, 'upVote');
}
if(e.target.getAttribute('name') === 'downVote' && likes > 0){
this.setState((perviouState)=>({
likes: perviouState.likes - 1
}));
handleLikeThunk(id, 'downVote');
}
}
为此,我将我的组件连接到商店
const mapDispatchToProps = (dispatch) => {
return {
handleLikeThunk: (id, likeType) => dispatch(handleLikeThunk(id,
likeType))
}
}
export default connect(null, mapDispatchToProps)(Post)
就像我说的,我希望能够在后端使用 thunk 函数 post 此信息,但它永远不会进入 thunk 函数内的 return statament。
看起来您正在调用原始函数,而不是您放入 mapDispatchToProps
的函数,请尝试将 handleLikeThunk(id, 'upVote');
更改为 this.props.handleLikeThunk(id, 'upVote');