Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-promptify
Advanced tools
Easily craft custom prompt modals in React with an intuitive API mirroring the classic prompt().
window.prompt
replacement for React. Render any prompt, return any value, right where you need it.
window.prompt
, the modal state is managed for youyarn add react-promptify
import { useState } from "react";
import { createPrompter } from "../lib/main";
const { Prompter, prompt } = createPrompter();
// Create a custom prompt function. We can call this anywhere in our app.
const promptEmail = () =>
prompt<boolean>((done) => (
<div>
<button onClick={() => done(true)}>Yes</button>
<button onClick={() => done(false)}>No</button>
</div>
));
function App() {
const [allowed, setAllowed] = useState(false);
return (
<div>
{/* Where the prompt will be rendered. */}
<Prompter>
{/* You can bring your own modal. The prompt and the open state are managed for you. */}
{({ children, open }) => <SomeModal open={open}>{children}</SomeModal>}
</Prompter>
<p>Marketing Emails Allowed: {allowed ? "Yes" : "No"}</p>
<button onClick={async () => setAllowed(await promptEmail())}>
Update Details
</button>
</div>
);
}
Ever noticed that whenever you just need to get a simple value from the user, you have to add a bunch of boilerplate to your app? You have to create a modal component, manage the open state in the parent component (open state + onClose handler), add the modal to the component tree, pass in a handler for the data, then when you need the data, open the modal, wait for the user to enter the data, and then handle the data. Aside from the added complexity, it creates a lot of distance between where you need the data and where you handle it.
With promptify, you define once where and how you want to render modals, and call the prompt()
function anywhere in your app to render a modal and get the data you need, where you need it. The modal state is managed for you, you can render any content, and you can return any value you want.
createPrompter()
Creates a prompter instance. You can create as many prompters as you want. This is useful because different kinds of prompts might need different kinds of modals.
Returns
An object with the following properties: Prompter
, prompt
.
Example
const { Prompter, prompt } = createPrompter();
const { Prompter: AlertPrompter, prompt: alert } = createPrompter();
Prompter
The component that renders the prompt. You can place it wherever you want the prompt to appear.
Props
children({children, open, cancel})
: A render function that takes children and open prop and returns a modal element.
children: ReactNode
: The prompt contents to render.open: boolean
: A boolean indicating whether the modal should be open or closed.cancel: () => void
: A function that closes the modal and resolves the promise with null
.Returns
The modal element to render.
Example
<Prompter>
{({ children, open, cancel }) => (
<SomeModal open={open} onClose={cancel}>
{children}
</SomeModal>
)}
</Prompter>
prompt(render)
A function you call to prompt the user. It works similarly to window.prompt
, except it is asynchronous and you can render whatever you want.
Parameters
render: (done) => ReactNode
: A render function that takes a done
function and returns the prompt contents to render. The done
function takes the data to return as argument.Returns
A promise that resolves to the value the done function was called with, or null
if the prompt was canceled.
Example
const answer = await prompt((done) => (
<div>
<button onClick={() => done("yes")}>Yes</button>
<button onClick={() => done("no")}>No</button>
</div>
));
Do not use hooks inside the render
function. This will cause undefined behavior. If you need to use hooks, create a separate component and render that instead.
Bad
const answer = await prompt((done) => {
const [value, setValue] = useState("");
return (
<div>
<input value={value} onChange={(e) => setValue(e.target.value)} />
<button onClick={() => done(value)}>Submit</button>
</div>
);
});
Good
const answer = await prompt((done) => <Prompt done={done} />);
function Prompt({ done }: { done: CallbackFn<string> }) {
const [value, setValue] = useState("");
return (
<div>
<input value={value} onChange={(e) => setValue(e.target.value)} />
<button onClick={() => done(value)}>Submit</button>
</div>
);
}
FAQs
Easily craft custom prompt modals in React with an intuitive API mirroring the classic prompt().
We found that react-promptify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.