useReduxContext.js 841 B

12345678910111213141516171819202122232425262728
  1. import { useContext } from 'react';
  2. import { ReactReduxContext } from '../components/Context';
  3. /**
  4. * A hook to access the value of the `ReactReduxContext`. This is a low-level
  5. * hook that you should usually not need to call directly.
  6. *
  7. * @returns {any} the value of the `ReactReduxContext`
  8. *
  9. * @example
  10. *
  11. * import React from 'react'
  12. * import { useReduxContext } from 'react-redux'
  13. *
  14. * export const CounterComponent = ({ value }) => {
  15. * const { store } = useReduxContext()
  16. * return <div>{store.getState()}</div>
  17. * }
  18. */
  19. export function useReduxContext() {
  20. var contextValue = useContext(ReactReduxContext);
  21. if (process.env.NODE_ENV !== 'production' && !contextValue) {
  22. throw new Error('could not find react-redux context value; please ensure the component is wrapped in a <Provider>');
  23. }
  24. return contextValue;
  25. }