From 670026d7f5a58ddeaad765f7ea0e2948204d8b7c Mon Sep 17 00:00:00 2001 From: "Markus J. Wetzel" Date: Thu, 23 Jan 2025 23:05:21 +0100 Subject: [PATCH] Remove prop types --- package.json | 7 ++--- src/Provider.js | 74 +++++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 5561052..4e42517 100644 --- a/package.json +++ b/package.json @@ -29,12 +29,9 @@ "files": [ "dist" ], - "dependencies": { - "prop-types": "^15.7.2" - }, "devDependencies": { - "packsy": "0.2.2", - "react": "16.13.1" + "packsy": "0.2.4", + "react": "18.3.1" }, "peerDependencies": { "react": ">=16.8" diff --git a/src/Provider.js b/src/Provider.js index 4905f39..1cd0f9a 100644 --- a/src/Provider.js +++ b/src/Provider.js @@ -1,15 +1,8 @@ import React, { useRef, useState, useMemo, useCallback } from 'react'; -import PropTypes from 'prop-types'; import StacksContext from './StacksContext'; import Timer from './Timer'; -const propTypes = { - autoDismiss: PropTypes.number.isRequired, - // eslint-disable-next-line react/forbid-prop-types - stacks: PropTypes.object.isRequired, - children: PropTypes.node.isRequired, -}; - +// eslint-disable-next-line react/prop-types function Provider({ autoDismiss, stacks: stacksConfig, children }) { const incrementalId = useRef(0); @@ -43,34 +36,39 @@ function Provider({ autoDismiss, stacks: stacksConfig, children }) { }); }, []); - const push = useCallback((name, component, config) => { - if (!stacks[name]) { - throw new Error(`Unknown stack "${name}"`); - } - - setStacks((prevStacks) => { - const stack = { ...prevStacks[name] }; - const item = { component, config }; - - // increment identifier - incrementalId.current += 1; - - const id = incrementalId.current; - - stack[id] = item; - - if (autoDismiss) { - stack[id].timer = new Timer(() => { - destroy(name, id); - }, autoDismiss); - } - - return { - ...prevStacks, - [name]: stack, - }; - }); - }, []); + const context = useMemo( + () => ({ + push: (name, component, config) => { + if (!stacks[name]) { + throw new Error(`Unknown stack "${name}"`); + } + + setStacks((prevStacks) => { + const stack = { ...prevStacks[name] }; + const item = { component, config }; + + // increment identifier + incrementalId.current += 1; + + const id = incrementalId.current; + + stack[id] = item; + + if (autoDismiss) { + stack[id].timer = new Timer(() => { + destroy(name, id); + }, autoDismiss); + } + + return { + ...prevStacks, + [name]: stack, + }; + }); + }, + }), + [], + ); return ( <> @@ -103,13 +101,11 @@ function Provider({ autoDismiss, stacks: stacksConfig, children }) { ); })} - + {children} ); } -Provider.propTypes = propTypes; - export default Provider;