gpt4 book ai didi

javascript - 未见 mixin 的状态

转载 作者:行者123 更新时间:2023-12-03 06:54:23 26 4
gpt4 key购买 nike

我有一个 mixin,它具有检查用户是否登录的方法:

authenticated: function() {
return this.state.currentUser !== null;
},

我将此 mixin 包含在另一个组件中,并像这样调用它:

mixins: [currentUserMixin],

checkUserAlreadyLoggedIn: function(nextState, replace) {
if(this.authenticated()) {
replace("/");
}
},

但是,在 Chrome 控制台中我收到此错误:

TypeError: Cannot read property 'currentUser' of null

看起来 mixin 的状态没有导入到其他组件的状态中。为什么会这样?

最佳答案

你必须确保你的初始状态对象存在于你的 mixin 或你的组件中。

getInitialState: function() {
return {
currentUser: null
};
}

参见:https://facebook.github.io/react/docs/component-specs.html#getinitialstate

关于javascript - 未见 mixin 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37348367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com