import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import * as React from 'react';

function useSafeState(initialState) {
  var _React$useState = React.useState(initialState),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      state = _React$useState2[0],
      setState = _React$useState2[1];

  var mounted = React.useRef(false);
  React.useEffect(function () {
    mounted.current = true;
    return function () {
      mounted.current = false;
    };
  }, []);
  var update = React.useCallback(function (nextState) {
    mounted.current && setState(nextState);
  }, []);
  return [state, update];
}

export default useSafeState;