Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@accurat/event-utils
Advanced tools
High order functions to use in React's JSX when working with events
High order functions to use in React's JSX when working with events
The objective of these functions is to reduce the boilerplate code when handling events in react, and to wire up the pure state setters directly to the component.
yarn add @accurat/event-utils
Given a basic and functional state setter in react
setName = (name) => {
this.setState({ name })
}
or the equivalent in mobx-state-tree
setName(name) {
self.name = name
}
with @accurat/event-utils
you can use the setter directly on the JSX component:
import { eventValueExtractor } from '@accurat/event-utils'
// ...
<input value={name} onChange={eventValueExtractor(setName)} />
Another common use case is with links and custom routing:
import { preventingDefault } from '@accurat/event-utils'
// ...
<a href={href} onClick={preventingDefault(gotoPage)} />
Also those functions use the powers of functional programming 🌪, so you can combine them!
import { preventingDefault, replaceArguments } from '@accurat/event-utils'
// ...
<div>
{options.map((option, i) =>
<a
href=""
key={i}
onClick={preventingDefault(replaceArguments(setName, option))}
>
{option}
</a>
)}
</div>
This way you don't have to use anymore the build pattern in react, and you can get rid of all those boilerplate methods in the component!
Another thing you can get rid of is the onKeyDown
method with the switch case inside that fires a function on a certain event.key
, you can use an object association instead:
import { attachListenersToKeys } from '@accurat/event-utils'
// ...
<input
type="text"
onKeyDown={attachListenersToKeys({
Enter: preventingDefault(state.commit),
ArrowDown: state.selectNext,
ArrowUp: state.selectPrev,
})}
/>
The available functions are:
lodash.partial
)If the names aren't self-explanatory enough, you can check out the source code, the functions are really simple and straightforward.
Note that react also supports passing null
to the event binding other than a function, so you will be also able to do
<a href="" onClick={preventingDefault(this.props.onClick)} />
allowing the component to receive null
as the onClick
prop, thanks to the line
if (fn === null) return null
FAQs
High order functions to use in React's JSX when working with events
The npm package @accurat/event-utils receives a total of 8 weekly downloads. As such, @accurat/event-utils popularity was classified as not popular.
We found that @accurat/event-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.