的我在终极版异步函数(终极版的thunk),在终极版店内设置的值。它在我的应用程序中被多次使用,但是,我希望在使用redux函数设置的新值运行redux函数后发生不同的事情。似乎是一个回调的好理由,对吧?所以,我想这样做只是这一点,但我得到了以下错误:在尝试修复
我认为的原因可能是,虽然终极版功能分派行动,这不是
Uncaught TypeError: Cannot read property 'then' of undefined
逻辑实际上没有任何回报。因此,我在redux函数中添加了returns
,认为该函数会将某些内容返回给调用它的组件,这将解决我收到的错误。没有运气。
问:
有谁知道我怎么能后执行功能异步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
})
}
})
}
}
终极版商店
const initialState = {
userHairColor: "",
userHeight: ""
}
export default function (state=initialState, action) {
switch(action.type) {
case 'SET':
return {
...state,
[action.id]: action.payload
}
default:
return {
...state
}
}
}
提前感谢所有帮助
'调度(集( 'userHairColor',userHairColor));''是一个set'行动创造者? 显示它,如果是。 – Andrew
这是一个减速器,我想看到行动的创造者。 – Andrew
添加了'SET'动作创建者 – Rbar