@tamagui/react-native-web-lite
Version:
React Native for Web
56 lines (55 loc) • 1.55 kB
JavaScript
import { invariant } from "@tamagui/react-native-web-internals";
import * as React from "react";
class StateSafePureComponent extends React.PureComponent {
_inAsyncStateUpdate = false;
constructor(props) {
super(props);
this._installSetStateHooks();
}
setState(partialState, callback) {
if (typeof partialState === "function") {
super.setState((state, props) => {
this._inAsyncStateUpdate = true;
let ret;
try {
ret = partialState(state, props);
} catch (err) {
throw err;
} finally {
this._inAsyncStateUpdate = false;
}
return ret;
}, callback);
} else {
super.setState(partialState, callback);
}
}
_installSetStateHooks() {
const that = this;
let {
props,
state
} = this;
Object.defineProperty(this, "props", {
get() {
invariant(!that._inAsyncStateUpdate, '"this.props" should not be accessed during state updates');
return props;
},
set(newProps) {
props = newProps;
}
});
Object.defineProperty(this, "state", {
get() {
invariant(!that._inAsyncStateUpdate, '"this.state" should not be acceessed during state updates');
return state;
},
set(newState) {
state = newState;
}
});
}
}
var StateSafePureComponent_default = StateSafePureComponent;
export { StateSafePureComponent, StateSafePureComponent_default as default };
//# sourceMappingURL=StateSafePureComponent.mjs.map