pesto-students/batch-11-relayer

View on GitHub
client/src/shared/PageProvider.js

Summary

Maintainability
A
1 hr
Test Coverage
import React from 'react';
import PropTypes from 'prop-types';

const PageContext = React.createContext(1);

const PageProvider = ({ children }) => {
  const [currentPage, setPage] = React.useState(1);
  const itemsPerPage = 5;
  const value = { itemsPerPage, currentPage, setPage };
  return (
    <PageContext.Provider value={value}>
      {children}
    </PageContext.Provider>
  );
};

const usePageContext = () => {
  const context = React.useContext(PageContext);
  if (!context) {
    throw new Error('Toggle component cannot be rendered outside the component');
  }
  return context;
};

PageProvider.propTypes = {
  children: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
  ]).isRequired,
};

export { PageProvider, usePageContext };