Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
use-binding
Advanced tools
One-line controlled and uncontrolled property binding for React
npm install use-binding
When developing React components, you often have to choose between providing a controlled (or "stateless") or uncontrolled ("stateful") version. For instance, if you have a custom Input component, do you:
defaultValue
prop and manage the state inside your component (uncontrolled), or do you:value
prop and expect the parent component to manage state for you (controlled)?You may end up developing both variants, adding lots of boilerplate code.
useBinding
allows you to do both in one line.
Use this in your component:
const [myValue, setMyValue] = useBinding(
defaultValue,
value,
onChange,
fallbackValue /* optional */
);
Now use myValue
as your property value, and use setMyValue
whenever you want to change it. Everything works as expected. That means:
value
, that value will be used.defaultValue
, then useBinding
will use useState
internally to keep track of the current value.defaultValue
and value
are empty, then the fallbackValue
will be used. The fallbackValue
is optional: if you don't provide it either, then myValue
will be null.onChange
handler, that handler will be called when using setMyValue
.Consider the Input
example above. You can use useBinding
like this:
import React from 'react';
import { useBinding } from 'use-binding';
interface IInputProps {
defaultValue?: string
value?: string
onChange?: (newValue: string) => void
}
const CustomInput: React.FC<IInputProps> = ({ defaultValue, value, onChange }) => {
const [inputValue, setInputValue] = useBinding(defaultValue, value, onChange, '');
return (
<input type="text" value={inputValue} onChange={(e: ChangeEvent<HTMLInputElement>) => { setInputValue(e.target.value) })} />
);
};
This will give consumers of your CustomInput
component a lot of options:
// Controlled:
const [value, setValue] = useState('');
<CustomInput value={value} onChange={setValue} />;
// Uncontrolled:
<CustomInput defaultValue="my default" onChange={(newValue: string) => { console.log(newValue); })} />;
// Fixed value:
<CustomInput value="can't change me" />;
useBinding
supports Typescript and contains generic typings. Of course you can also use it in plain old Javascript.
Developed by Sebastiaan Besselsen at Sdu Uitgevers, The Netherlands.
FAQs
Unknown package
The npm package use-binding receives a total of 202 weekly downloads. As such, use-binding popularity was classified as not popular.
We found that use-binding demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.