Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
react-expiry
Advanced tools
Hide React components based on expiry date/time.
pnpm add react-expiry
// _app.tsx
import { ExpiryProvider } from "react-expiry";
import { Hello } from "./hello";
const App = () => {
return (
<ExpiryProvider>
<Hello />
</ExpiryProvider>
);
};
// hello.tsx
import { useExpiry } from "react-expiry";
const Hello = () => {
const showWelcomeMsg = useExpiry({
id: "welcome-msg",
ttl: 24 * 60 * 60, // 1 day in seconds
});
const showLaunchDate = useExpiry({
id: "launch-date",
expires: new Date("2024-10-03"),
});
const showNewFeature = useExpiry({
id: "new-feature",
expires: new Date("2025-01-01"),
ttl: 5 * 24 * 60 * 60, // 5 days in seconds
});
return (
<div>
<h1>Hello World</h1>
{showWelcomeMsg && (
<p>After initial render, I will disappear after 1 day.</p>
)}
{showLaunchDate && (
<p>I will always disappear by 3rd of October, 2024.</p>
)}
{showNewFeature && (
<p>
After initial render, I will disappear after 5 days or by 1st of
January, 2025, whichever comes first.
</p>
)}
</div>
);
};
ExpiryProvider
defaultTTL
(default: 0
)This is the default time-to-live (in seconds) for components. If a hook
has a ttl
option set, it will override this value.
keyPrefix
(default: "expiry:"
)This is the prefix for the keys in the storage. It is used to avoid conflicts between different components.
browserOnly
(default: true
)Controls the rendering behavior when rendering on the server. If true
, the
component will only be rendered on the browser, otherwise it will be rendered
on both the browser and the server.
storage
(default: localStorage
)This is the storage used to persist the expiry. It defaults to localStorage
.
useExpiry
id
(required)This is the identifier for the expiry and is used in the persistence key. It should be unique within your application.
ttl
(optional)This is the time-to-live (in seconds) for the expiry. When the ttl
is reached, the expiry will return false
and the component will be hidden.
NOTE: The use of
ttl
is per user session. If the user clears their storage, the expiry will be reset and thettl
will start again.
expires
(optional)This is the expiration date for the expiry. When the expires
date is reached, the expiry will return false
and the component will be hidden.
NOTE: The use of
expires
is global. It will hide the component for all users.
MIT
You can install the companion ESLint plugin to add react-expiry
rules to your linting.
pnpm add eslint-plugin-react-expiry
Then add these rules to your ESLint config.
{
"plugins": ["react-expiry"],
"rules": {
"react-expiry/expires-in-past": "warn",
"react-expiry/duplicate-id": "warn"
}
}
FAQs
Hide React components based on expiry date/time
The npm package react-expiry receives a total of 39 weekly downloads. As such, react-expiry popularity was classified as not popular.
We found that react-expiry demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.