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.
@cruise-automation/hooks
Advanced tools
A set of resusable React hooks.
npm install --save @cruise-automation/hooks
useAbortable
A React hook to load async, disposable resources and fire the cleanup callback when the component unmountts. If the component unmounts before the async operation completes, the resource will still be cleaned up once it finishes loading and an abort signal will be issued to the async load operation.
// types
useAbortable<T>(
defaultValue: T, // default value, will be set to the actual value if action is performed successfully
action: (AbortController) => Promise<T>, // async action to perform
cleanup: (?T) => void, // clean up work if thtere is any
args: any // dependencies
): [T, () => void] // result value and abort function
// sample usage
import { useAbortable } from "@cruise-automation/hooks";
function Example(props) {
const [data, setData] = useState(null);
const dateName = 'foo';
// fetch data from remote when the component is mounted
const [remoteData, abortFn] = useAbortable(
null,
async (abortController) => {
if (dataName) {
fetchDataFromRemote(props, dataName)
}
}
(val) => {},
[dataName]
);
// abort is usually called automatically when the component unmounts, but it can also be called manually
function abortManually() {
abortFn();
}
return (
<div>
<button onClick={abortManually}>Stop Loading</button>
</div>
);
}
useCleanup
A small React hook to fire the cleanup callback when the component unmounts. Equivalent to useEffect(() => () => { teardown(); }, [])
.
// types
useCleanup(teardown: () => void): void
// sample usage
function Example() {
const [audioContext] = React.useState(() => new window.AudioContext));
// automatically close audioContext when the component unmounts
useCleanup(() => audioContext.close());
return null;
}
useEventListener
A hook for conditionally adding and removing event listeners on DOM elements.
// types
useEventListener<T>(
target: Element, // event target, e.g. window
type: string, // event type
enable: boolean,
handler: (any) => void,
dependencies: any[],
): void
// sample usage
function Example() {
const [isDragging, setIsDragging] = useState(false);
useEventListener(
window,
"mousemove",
isDragging, // add this listener only during mouse dragging
(event) => {
/* do something here... */
},
[]
);
return <div onMouseDown={() => setIsDragging(true)}>demo</div>;
}
useAnimationFrame
A React hook that accepts a callback function which will be called repeatedly, synchronized with the browser's repaints via requestAnimationFrame
.
// types
useAnimationFrame(
callback: (timestamp: number) => void,
disable: boolean,
dependencies: any[],
): void
// sample usage: a count state that gets updated in every animation frame
function Example() {
const [count, setCount] = React.useState(0);
useAnimationFrame(
(timestamp) => {
setCount(count + 1);
},
false,
[]
);
return <span>{count}</span>;
}
FAQs
A set of resusable React hooks
The npm package @cruise-automation/hooks receives a total of 99 weekly downloads. As such, @cruise-automation/hooks popularity was classified as not popular.
We found that @cruise-automation/hooks 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.