Reactjs ReactNative:this.setState错误:null不是对象

Reactjs ReactNative:this.setState错误:null不是对象,reactjs,react-native,Reactjs,React Native,反应脚本 class TransactionsList extends Component { constructor(props) { super(props); this.state = { activeAccountId: "", accessToken: "", TransactionsData: "", }; } replaceRoute(ro

反应脚本

class TransactionsList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            activeAccountId: "",
            accessToken: "",
            TransactionsData: "",
        };
    }
    replaceRoute(route, passProps) {
        this.props.replaceRoute(route, passProps);
    }
    async _getToken() {
        try {
            let accessToken = await AsyncStorage.getItem('AUTH_TOKEN');
            if(!accessToken) {
                this.replaceRoute('login');
            } else {
                this.setState({accessToken: accessToken})
            }
        } catch(error) {
            Alert.alert('Print Errorr', error.message)
            this.replaceRoute('login');
        }
    }
    componentWillMount(){
        this._getToken()
        let token = 'Token '+this.state.accessToken
        this.load_data(token)
    }
    render() {
        return (
            <Container>
            // other code
            </Container>
        )
    }
}
类事务列表扩展组件{
建造师(道具){
超级(道具);
此.state={
activeAccountId:“”,
accessToken:“”,
TransactionData:“”,
};
}
更换路线(路线,通行证){
this.props.replaceRoute(路径,passrops);
}
异步_getToken(){
试一试{
让accessToken=等待AsyncStorage.getItem('AUTH_TOKEN');
如果(!accessToken){
此.replaceRoute('login');
}否则{
this.setState({accessToken:accessToken})
}
}捕获(错误){
Alert.Alert('Print error',error.message)
此.replaceRoute('login');
}
}
组件willmount(){
这个。_getToken()
let token='token'+this.state.accessToken
此.load_数据(令牌)
}
render(){
返回(
//其他代码
)
}
}
getToken中的setState中出现错误下面是catch(error)块输出

打印错误null不是对象(正在评估) prevComponentInstance.\u currentElement)


但上述代码在其他屏幕中也可以使用。

您需要使用如下方式绑定
\u getToken
方法:

this._getToken().bind(this)
或者您可以在构造函数中执行此操作以获得更好的代码(我更喜欢此代码):


希望这有帮助

不建议在
componentWillMount
中进行api调用,因为当api调用完成并且您调用
setState
时,组件可能尚未装入

相反,您应该在
componentDidMount
中进行api调用。根据报告:

componentDidMount()在创建组件后立即调用 安装。需要DOM节点的初始化应该在这里进行。如果你 需要从远程端点加载数据,这是一个好地方 实例化网络请求。此方法中的设置状态将 触发重新渲染


而且,您还需要像@Jazib提到的那样绑定。

我知道我的回复有点晚,但更好的方法是使用arrow函数,而不是对命名函数使用bind。因此,您可以这样编写
\u getToken
方法:

this._getToken().bind(this)
const\u getToken=async()=>{
//你的逻辑在这里
}

这里的arrow函数隐式地将组件的当前实例分配给
this
关键字,而在命名函数中,您必须使用其他人提到的
bind
方法为
this
关键字提供上下文


另外,
componentWillMount
现在已被弃用,如果您在
componentDidMount

中调用您的方法会更好,也许您需要在调用
getToken
方法时显式绑定
this
this.\u getToken().bind(this)
@BasimHennawi尝试添加
this.\u getToken=this.\u getToken().bind(这)
在构造函数方法中。Get error setState只能在已安装的组件上调用。确切地说,您只能更改状态(通过
setState
方法)在已装入的组件上,因此无论是在构造函数中还是在
组件上,如果您传递了道具,都将接收道具。我需要访问
标记
和componentWillmount方法中的一些其他变量,以从API获取数据。我尝试使用setState。任何其他解决方案。在
componentWillmount
中更好地调度操作,即hat
redux
framework是为帮助您的数据更改成为单向流而设计的。然后有
reducer
侦听这些更改并更新您的状态。我建议您阅读
Flux
redux
framework.added
this.\u getToken=this.\u getToken().bind(this)
在构造函数方法中。收到警告:setState(…):只能更新装入组件的装入组件。我认为这可能与您为获取令牌而进行的异步调用有关。虽然允许在componentWillMount中设置状态,并且不应发出警告,但我认为在这种情况下,当令牌的结果返回时,您的代码可能已经进入呈现阶段。尝试移动此项。_getToken()调用componentDidMount()。