UNPKG

@tamagui/react-native-web-lite

Version:
47 lines (46 loc) 1.36 kB
import { invariant } from "@tamagui/react-native-web-internals"; import * as React from "react"; class StateSafePureComponent extends React.PureComponent { _inAsyncStateUpdate = !1; constructor(props) { super(props), this._installSetStateHooks(); } setState(partialState, callback) { typeof partialState == "function" ? super.setState((state, props) => { this._inAsyncStateUpdate = !0; let ret; try { ret = partialState(state, props); } catch (err) { throw err; } finally { this._inAsyncStateUpdate = !1; } return ret; }, callback) : super.setState(partialState, callback); } _installSetStateHooks() { const that = this; let { props, state } = this; Object.defineProperty(this, "props", { get() { return invariant(!that._inAsyncStateUpdate, '"this.props" should not be accessed during state updates'), props; }, set(newProps) { props = newProps; } }), Object.defineProperty(this, "state", { get() { return invariant(!that._inAsyncStateUpdate, '"this.state" should not be acceessed during state updates'), state; }, set(newState) { state = newState; } }); } } export { StateSafePureComponent as default }; //# sourceMappingURL=StateSafePureComponent.mjs.map