UNPKG

inject-react-unmont

Version:

'解决异步请求时,出现不能在已经被销毁的组件中调用setState()方案。Can’t call setState (or forceUpdate) on an unmounted component '

18 lines (16 loc) 641 B
//import React, { Component } from 'react' function inject_react_unmont (target) { // 改装componentWillUnmount,销毁的时候记录一下 let next = target.prototype.componentWillUnmount target.prototype.componentWillUnmount = function () { if (next) next.call(this, ...arguments); this.unmount = true } // 对setState的改装,setState查看目前是否已经销毁 let setState = target.prototype.setState target.prototype.setState = function () { if ( this.unmount ) return ; setState.call(this, ...arguments) } } export default inject_react_unmont