Redux Thunk 函数之后的回调:未捕获类型错误:无法读取未定义的属性'then'



>问题

我在 redux 中有一个异步函数(带有 redux thunk(,它在 redux 存储中设置了一个值。 它在我的应用程序中被多次使用,但是,我希望在使用 redux 函数设置的新值运行 redux 函数后发生不同的事情。 这似乎是回调的一个很好的理由,对吧? 所以我尝试这样做,但我得到以下错误:

捕获的类型错误:无法读取未定义的属性"then">

尝试修复的逻辑

我认为原因可能是尽管 redux 函数调度操作,但它实际上并没有返回任何内容。 因此,我在 redux 函数中添加了returns,认为该函数会将一些内容返回给调用它的组件,这将解决我收到的错误。 没有运气。


问题:

有谁知道在异步 redux 函数(使用 redux thunk(运行并完成在 redux 存储中设置新值后如何执行函数?


我最近一次尝试解决

元件

import {fetchUser} from './../actions/index.js';
class MyComponent extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
// Attempt #1: this.props.fetchUserData()); // originally I attempted just running one function and then the rest without a callback but the new value was not set in redux store before the next function that needed it ran
// Attempt #2: this.props.fetchUserData().then(function() => {  // I also tried running just a standard .then(function()
// Attempt #3: (line below) 
this.props.fetchUserData().then((resultFromReduxFunction) => {
console.log("fetchUserData result = " + resultFromReduxFunction);
// do some stuff with the new data
console.log("this.props.reduxData.userHairColor = " + this.props.reduxData.userHairColor);
console.log("this.props.reduxData.userHeight = " + this.props.reduxData.userHeight);
});
}
}
function mapStateToProps(state) {
return {
reduxData: state.user
};
}
function matchDispatchToProps(dispatch) {
return bindActionCreators({
fetchUserData: fetchUserData
}, dispatch)
}
export default connect(mapStateToProps, matchDispatchToProps)(MyComponent);

动作创建者

export const set = (idToSet, payloadToSet) => {
return {
type: 'SET',
id: idToSet,
payload: payloadToSet
}
}
export const fetchUserData = (callbackFunction) => {
console.log("fetchUserData triggered...");
return (dispatch, getState) => {
firebase.auth().onAuthStateChanged(function (user) {
if (!user) {
console.log("no user logged in");
return false // Added in attempt to fix callback 
} else {
// fetch some more user data
firebase.database().ref().child('users').child(user.uid).on('value', function(snapshot) {
var userHairColor = snapshot.child(userHairColor).val();
var userHeight = snapshot.child(userHeight).val();
dispatch(set('userHairColor', userHairColor));
dispatch(set('userHeight', userHeight));
return true // Added in attempt to fix callback 
})
}
})
}
}

Redux Store

const initialState = {
userHairColor: "",
userHeight: ""
}
export default function (state=initialState, action) {
switch(action.type) {
case 'SET':
return {
...state,
[action.id]: action.payload
}
default:
return {
...state
}
}
}

提前感谢您的任何帮助

要使用.then(...),你的thunk必须返回一个承诺。Firebase 的onAuthStateChanged似乎返回的是取消订阅函数,而不是 Promise,因此即使您返回了该函数,它也不允许您在操作创建者调用站点上链接其他回调。而且你不能从传递给onAuthStateChanged的回调中返回,因为此时你在不同的调用堆栈中(它是异步的(。

你要做的是将一个回调函数传递给你的动作创建者,它需要在数据获取的成功回调中调用该函数。

像这样:

export const fetchUserData = (callbackFunction) => {
return (dispatch, getState) => {
firebase.auth().onAuthStateChanged(function (user) {
if (!user) {
console.log("no user logged in");
// You can pass false to your callback here, so that it knows the auth was unsuccessful
callbackFunction(false); 
} else {
// fetch some more user data
firebase.database().ref().child('users').child(user.uid).on('value', function(snapshot) {
var userHairColor = snapshot.child(userHairColor).val();
var userHeight = snapshot.child(userHeight).val();
dispatch(set('userHairColor', userHairColor));
dispatch(set('userHeight', userHeight));
// Passing true so your callback knows it was a success
callbackFunction(true); 
})
}
})
}
}

然后根据需要使用它:

this.props.fetchUserData((success) => {
console.log("fetchUserData result = " + success);
// do some stuff with the new data
console.log("this.props.reduxData.userHairColor = " + this.props.reduxData.userHairColor);
console.log("this.props.reduxData.userHeight = " + this.props.reduxData.userHeight);
});

这里有一些回调地狱,但为了避免这种情况,你必须使用Firebase的API的"承诺"版本,在这种情况下,你也可以从你的thunk中返回承诺,以便任何使用你的动作创建者的东西都可以附加一个.then(...)

如果你实际上在 thunk 中显式调用它,回调会起作用,但它真的感觉像是一种反模式。

相反,您可以派遣另一个可以处理任何进一步逻辑的 thunk。

export const fetchUserData = (callbackFunction) => {
console.log("fetchUserData triggered...");
return (dispatch, getState) => {
firebase.auth().onAuthStateChanged(function (user) {
...do stuf...
dispatch(doMoreStuff())
})
}
}

或者,如果您需要对 react 组件中的结果做出反应,请调度一个将修改 redux 状态的操作。这将反过来调用 react 生命周期方法和render,您可以根据新状态对那里的更改做出反应。

为了改进答案,了解操作完成后要做什么会有所帮助。

相关内容

最新更新