Javascript React Native-Undefined不是对象(计算this.props.isLoggedIn.then)
我是react native的初学者,我正在开发一个小应用程序来显示firebase数据库的提要,如果用户登录的话。所以在调用isloggedin函数之后,我使用then函数来显示firebase数据 这是my isLoggedIn函数:Javascript React Native-Undefined不是对象(计算this.props.isLoggedIn.then),javascript,reactjs,react-native,Javascript,Reactjs,React Native,我是react native的初学者,我正在开发一个小应用程序来显示firebase数据库的提要,如果用户登录的话。所以在调用isloggedin函数之后,我使用then函数来显示firebase数据 这是my isLoggedIn函数: export const isLoggedIn = () => { return dispatch => { firebase.auth().onAuthStateChanged(user => { if(user){
export const isLoggedIn = () => {
return dispatch => {
firebase.auth().onAuthStateChanged(user => {
if(user){
Actions.main();
dispatch({
type:LOGGED_IN
})
}else {
Actions.auth();
dispatch({
type:NOT_LOGGED_IN
})
}
});
}
};
我在componentDidMount函数中调用isLoggedIn函数
componentDidMount (){
this.props.isLoggedIn()
.then(() => {
this.props.fetchTweet();
});
}
但我得到了如下错误:undefined不是一个对象(计算'this.props.isLoggedIn().then')
我希望从我的代码中显示我的firebase数据。
顺便说一句,fetchTweet在没有then函数的情况下工作,isLoogedin也可以工作,如果我不使用“then”这将不起作用,因为
This.props.isLoggedIn()
返回一个接受分派对象的函数。此操作将被redux thunk(中间件)拦截。因此,这不是回报承诺,因此它不可支持。它将产生错误。正确的方法是:
componentDidMount (){
// calling only the action creator
this.props.isLoggedIn()
}
倾听减速器中的动作,如:
// Typical reducer
function(state = INITIAL_STATE, action) {
switch(action.type) {
case LOGGED_IN :
// change your state according to the business logic
// for example make a flag IsLoggedIn and make it true
const changed_state = // Your changed state
return changed_state
default:
return state;
}
将状态从mapStateToProps注入组件,然后执行逻辑,然后执行this.props.fetchTweet()
然后期待一个承诺,你会还一个吗?我从来没有用过承诺,我总是这样打电话,但这一次真的没用!!??谢谢:)
// Implementing mapStateToProps function
const mapStateToProps = function(state) {
// get the key from the state
const loginSuccess = state.IsLoggedIn
return {
loginSuccess
}
componentWilReceiveProps(nextProps) {
if(nextProps.loginSuccess && this.props.loginSuccess !== nextProps.loginSuccess) {
this.props.fetchTweet()
}
}