This commit is contained in:
52
node_modules/react-redux/lib/hooks/useDispatch.js
generated
vendored
Normal file
52
node_modules/react-redux/lib/hooks/useDispatch.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.createDispatchHook = createDispatchHook;
|
||||
exports.useDispatch = void 0;
|
||||
|
||||
var _Context = require("../components/Context");
|
||||
|
||||
var _useStore = require("./useStore");
|
||||
|
||||
/**
|
||||
* Hook factory, which creates a `useDispatch` hook bound to a given context.
|
||||
*
|
||||
* @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
|
||||
* @returns {Function} A `useDispatch` hook bound to the specified context.
|
||||
*/
|
||||
function createDispatchHook(context) {
|
||||
if (context === void 0) {
|
||||
context = _Context.ReactReduxContext;
|
||||
}
|
||||
|
||||
var useStore = context === _Context.ReactReduxContext ? _useStore.useStore : (0, _useStore.createStoreHook)(context);
|
||||
return function useDispatch() {
|
||||
var store = useStore();
|
||||
return store.dispatch;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* A hook to access the redux `dispatch` function.
|
||||
*
|
||||
* @returns {any|function} redux store's `dispatch` function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import React, { useCallback } from 'react'
|
||||
* import { useDispatch } from 'react-redux'
|
||||
*
|
||||
* export const CounterComponent = ({ value }) => {
|
||||
* const dispatch = useDispatch()
|
||||
* const increaseCounter = useCallback(() => dispatch({ type: 'increase-counter' }), [])
|
||||
* return (
|
||||
* <div>
|
||||
* <span>{value}</span>
|
||||
* <button onClick={increaseCounter}>Increase counter</button>
|
||||
* </div>
|
||||
* )
|
||||
* }
|
||||
*/
|
||||
|
||||
|
||||
var useDispatch = /*#__PURE__*/createDispatchHook();
|
||||
exports.useDispatch = useDispatch;
|
||||
34
node_modules/react-redux/lib/hooks/useReduxContext.js
generated
vendored
Normal file
34
node_modules/react-redux/lib/hooks/useReduxContext.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.useReduxContext = useReduxContext;
|
||||
|
||||
var _react = require("react");
|
||||
|
||||
var _Context = require("../components/Context");
|
||||
|
||||
/**
|
||||
* A hook to access the value of the `ReactReduxContext`. This is a low-level
|
||||
* hook that you should usually not need to call directly.
|
||||
*
|
||||
* @returns {any} the value of the `ReactReduxContext`
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import React from 'react'
|
||||
* import { useReduxContext } from 'react-redux'
|
||||
*
|
||||
* export const CounterComponent = ({ value }) => {
|
||||
* const { store } = useReduxContext()
|
||||
* return <div>{store.getState()}</div>
|
||||
* }
|
||||
*/
|
||||
function useReduxContext() {
|
||||
var contextValue = (0, _react.useContext)(_Context.ReactReduxContext);
|
||||
|
||||
if (process.env.NODE_ENV !== 'production' && !contextValue) {
|
||||
throw new Error('could not find react-redux context value; please ensure the component is wrapped in a <Provider>');
|
||||
}
|
||||
|
||||
return contextValue;
|
||||
}
|
||||
170
node_modules/react-redux/lib/hooks/useSelector.js
generated
vendored
Normal file
170
node_modules/react-redux/lib/hooks/useSelector.js
generated
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.createSelectorHook = createSelectorHook;
|
||||
exports.useSelector = void 0;
|
||||
|
||||
var _react = require("react");
|
||||
|
||||
var _useReduxContext2 = require("./useReduxContext");
|
||||
|
||||
var _Subscription = require("../utils/Subscription");
|
||||
|
||||
var _useIsomorphicLayoutEffect = require("../utils/useIsomorphicLayoutEffect");
|
||||
|
||||
var _Context = require("../components/Context");
|
||||
|
||||
var refEquality = function refEquality(a, b) {
|
||||
return a === b;
|
||||
};
|
||||
|
||||
function useSelectorWithStoreAndSubscription(selector, equalityFn, store, contextSub) {
|
||||
var _useReducer = (0, _react.useReducer)(function (s) {
|
||||
return s + 1;
|
||||
}, 0),
|
||||
forceRender = _useReducer[1];
|
||||
|
||||
var subscription = (0, _react.useMemo)(function () {
|
||||
return (0, _Subscription.createSubscription)(store, contextSub);
|
||||
}, [store, contextSub]);
|
||||
var latestSubscriptionCallbackError = (0, _react.useRef)();
|
||||
var latestSelector = (0, _react.useRef)();
|
||||
var latestStoreState = (0, _react.useRef)();
|
||||
var latestSelectedState = (0, _react.useRef)();
|
||||
var storeState = store.getState();
|
||||
var selectedState;
|
||||
|
||||
try {
|
||||
if (selector !== latestSelector.current || storeState !== latestStoreState.current || latestSubscriptionCallbackError.current) {
|
||||
var newSelectedState = selector(storeState); // ensure latest selected state is reused so that a custom equality function can result in identical references
|
||||
|
||||
if (latestSelectedState.current === undefined || !equalityFn(newSelectedState, latestSelectedState.current)) {
|
||||
selectedState = newSelectedState;
|
||||
} else {
|
||||
selectedState = latestSelectedState.current;
|
||||
}
|
||||
} else {
|
||||
selectedState = latestSelectedState.current;
|
||||
}
|
||||
} catch (err) {
|
||||
if (latestSubscriptionCallbackError.current) {
|
||||
err.message += "\nThe error may be correlated with this previous error:\n" + latestSubscriptionCallbackError.current.stack + "\n\n";
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
||||
(0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(function () {
|
||||
latestSelector.current = selector;
|
||||
latestStoreState.current = storeState;
|
||||
latestSelectedState.current = selectedState;
|
||||
latestSubscriptionCallbackError.current = undefined;
|
||||
});
|
||||
(0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(function () {
|
||||
function checkForUpdates() {
|
||||
try {
|
||||
var newStoreState = store.getState(); // Avoid calling selector multiple times if the store's state has not changed
|
||||
|
||||
if (newStoreState === latestStoreState.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
var _newSelectedState = latestSelector.current(newStoreState);
|
||||
|
||||
if (equalityFn(_newSelectedState, latestSelectedState.current)) {
|
||||
return;
|
||||
}
|
||||
|
||||
latestSelectedState.current = _newSelectedState;
|
||||
latestStoreState.current = newStoreState;
|
||||
} catch (err) {
|
||||
// we ignore all errors here, since when the component
|
||||
// is re-rendered, the selectors are called again, and
|
||||
// will throw again, if neither props nor store state
|
||||
// changed
|
||||
latestSubscriptionCallbackError.current = err;
|
||||
}
|
||||
|
||||
forceRender();
|
||||
}
|
||||
|
||||
subscription.onStateChange = checkForUpdates;
|
||||
subscription.trySubscribe();
|
||||
checkForUpdates();
|
||||
return function () {
|
||||
return subscription.tryUnsubscribe();
|
||||
};
|
||||
}, [store, subscription]);
|
||||
return selectedState;
|
||||
}
|
||||
/**
|
||||
* Hook factory, which creates a `useSelector` hook bound to a given context.
|
||||
*
|
||||
* @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
|
||||
* @returns {Function} A `useSelector` hook bound to the specified context.
|
||||
*/
|
||||
|
||||
|
||||
function createSelectorHook(context) {
|
||||
if (context === void 0) {
|
||||
context = _Context.ReactReduxContext;
|
||||
}
|
||||
|
||||
var useReduxContext = context === _Context.ReactReduxContext ? _useReduxContext2.useReduxContext : function () {
|
||||
return (0, _react.useContext)(context);
|
||||
};
|
||||
return function useSelector(selector, equalityFn) {
|
||||
if (equalityFn === void 0) {
|
||||
equalityFn = refEquality;
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (!selector) {
|
||||
throw new Error("You must pass a selector to useSelector");
|
||||
}
|
||||
|
||||
if (typeof selector !== 'function') {
|
||||
throw new Error("You must pass a function as a selector to useSelector");
|
||||
}
|
||||
|
||||
if (typeof equalityFn !== 'function') {
|
||||
throw new Error("You must pass a function as an equality function to useSelector");
|
||||
}
|
||||
}
|
||||
|
||||
var _useReduxContext = useReduxContext(),
|
||||
store = _useReduxContext.store,
|
||||
contextSub = _useReduxContext.subscription;
|
||||
|
||||
var selectedState = useSelectorWithStoreAndSubscription(selector, equalityFn, store, contextSub);
|
||||
(0, _react.useDebugValue)(selectedState);
|
||||
return selectedState;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* A hook to access the redux store's state. This hook takes a selector function
|
||||
* as an argument. The selector is called with the store state.
|
||||
*
|
||||
* This hook takes an optional equality comparison function as the second parameter
|
||||
* that allows you to customize the way the selected state is compared to determine
|
||||
* whether the component needs to be re-rendered.
|
||||
*
|
||||
* @param {Function} selector the selector function
|
||||
* @param {Function=} equalityFn the function that will be used to determine equality
|
||||
*
|
||||
* @returns {any} the selected state
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import React from 'react'
|
||||
* import { useSelector } from 'react-redux'
|
||||
*
|
||||
* export const CounterComponent = () => {
|
||||
* const counter = useSelector(state => state.counter)
|
||||
* return <div>{counter}</div>
|
||||
* }
|
||||
*/
|
||||
|
||||
|
||||
var useSelector = /*#__PURE__*/createSelectorHook();
|
||||
exports.useSelector = useSelector;
|
||||
52
node_modules/react-redux/lib/hooks/useStore.js
generated
vendored
Normal file
52
node_modules/react-redux/lib/hooks/useStore.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.createStoreHook = createStoreHook;
|
||||
exports.useStore = void 0;
|
||||
|
||||
var _react = require("react");
|
||||
|
||||
var _Context = require("../components/Context");
|
||||
|
||||
var _useReduxContext2 = require("./useReduxContext");
|
||||
|
||||
/**
|
||||
* Hook factory, which creates a `useStore` hook bound to a given context.
|
||||
*
|
||||
* @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
|
||||
* @returns {Function} A `useStore` hook bound to the specified context.
|
||||
*/
|
||||
function createStoreHook(context) {
|
||||
if (context === void 0) {
|
||||
context = _Context.ReactReduxContext;
|
||||
}
|
||||
|
||||
var useReduxContext = context === _Context.ReactReduxContext ? _useReduxContext2.useReduxContext : function () {
|
||||
return (0, _react.useContext)(context);
|
||||
};
|
||||
return function useStore() {
|
||||
var _useReduxContext = useReduxContext(),
|
||||
store = _useReduxContext.store;
|
||||
|
||||
return store;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* A hook to access the redux store.
|
||||
*
|
||||
* @returns {any} the redux store
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import React from 'react'
|
||||
* import { useStore } from 'react-redux'
|
||||
*
|
||||
* export const ExampleComponent = () => {
|
||||
* const store = useStore()
|
||||
* return <div>{store.getState()}</div>
|
||||
* }
|
||||
*/
|
||||
|
||||
|
||||
var useStore = /*#__PURE__*/createStoreHook();
|
||||
exports.useStore = useStore;
|
||||
Reference in New Issue
Block a user