UNPKG

2.11 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var slicedToArray = require('./slicedToArray-0711941d.js');
8require('./unsupportedIterableToArray-68db1d3b.js');
9var React = require('react');
10var React__default = _interopDefault(React);
11
12function initContainsComponent() {
13 var ContainsContext = React__default.createContext({
14 contains: false
15 });
16 return {
17 // Wrap the parent component with this provider.
18
19 /* eslint-disable react/prop-types */
20 Provider: function Provider(_ref) {
21 var children = _ref.children;
22
23 var _useState = React.useState(0),
24 _useState2 = slicedToArray._slicedToArray(_useState, 2),
25 count = _useState2[0],
26 setCount = _useState2[1];
27
28 var contextValue = React.useMemo(function () {
29 return {
30 updateCount: function updateCount(diff) {
31 setCount(function (count) {
32 return count + diff;
33 });
34 },
35 contains: count > 0
36 };
37 }, [count]);
38 return /*#__PURE__*/React__default.createElement(ContainsContext.Provider, {
39 value: contextValue
40 }, children);
41 },
42
43 /* eslint-enable react/prop-types */
44 // Call this from the parent component (returns a boolean)
45 useContains: function useContains() {
46 return React.useContext(ContainsContext).contains;
47 },
48 // Call this from the child component
49 useRegister: function useRegister() {
50 var _useContext = React.useContext(ContainsContext),
51 updateCount = _useContext.updateCount;
52
53 React.useEffect(function () {
54 if (!updateCount) {
55 return;
56 }
57
58 updateCount(1);
59 return function () {
60 updateCount(-1);
61 }; // We don't want this effect to ever re-run
62 // eslint-disable-next-line react-hooks/exhaustive-deps
63 }, []);
64 }
65 };
66}
67
68exports.initContainsComponent = initContainsComponent;
69//# sourceMappingURL=contains-component.js.map