
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@avidian/hooks
Advanced tools
React helper hooks for convenience
npm
npm install @avidian/hooks
yarn
yarn add @avidian/hooks
Used to generate route path relative to the current component and route. (react-router-dom or react-router-native)
Example (using react-router-dom)
import React, { FC } from 'react';
import { Switch, Route } from 'react-router-dom';
import { useURL } from '@avidian/hooks';
import Form from './Form';
import List from './List';
export default function Component(props) => {
const url = useURL();
return (
<Switch>
<Route path={url('')} exact component={List} />
<Route path={url('/add')} component={Form} />
<Route path={url('/:id/edit')} component={Form} />
</Switch>
);
};
Used for determining form mode (Add or Edit). Defaults to 'Add'
import React from 'react';
import { useMode } from '@avidian/hooks';
export default function Form(props) {
const [mode, setMode] = useMode();
const match = useRouteMatch<{ id: string }>();
const id = match.params.id;
const fetchData = async (id) => {
//
};
useEffect(() => {
if (match.path.includes('edit')) {
setMode('Edit');
fetchData(id);
}
}, []);
return (
<div>
{mode} Data
<form>
<input />
<button type='submit' >submit</button>
</form>
</div>
);
}
Define state that can be null.
import React from 'react';
import { useNullable } from '@avidian/hooks';
export default function Component() {
const [numberOrNull, setNumberOrNull] = useNullable(15);
return (
<button onClick={() => {
// errors if you're using typescript
setNumberOrNull('1');
setNumberOrNull({});
setNumberOrNull();
// works
setNumberOrNull(0);
setNumberOrNull(null);
}}>
click me
</button>
);
}
Define an array as state.
import React from 'react';
import { useArray } from '@avidian/hooks';
export default function Component() {
// defaults to an array instead of undefined
const [array, setArray] = useArray();
// ...
}
Define an array as state with it's helper methods. Using the helper methods will automatically mutate the array state for you.
import React from 'react';
import { useComplexArray } from '@avidian/hooks';
export default function Component() {
const { array, set, push, filter, update, remove, clear } = useComplexArray();
// ...
}
It's the same as useState({})
but it's useful in typescript, it makes the properties of the object optional with the Partial<T>
generic.
import React from 'react';
// required type
type Data = {
title: string;
description: string;
}
export default function Component() {
const [value, setValue] = usePartial<Data>({
// passing an object is optional
title: 'My Title',
});
// ...
}
Define a toggleable boolean state.
import React from 'react';
import { useToggle } from '@avidian/hooks';
export default function Component() {
const [value, toggleValue] = useToggle(false);
// explicitly set value
toggleValue(true);
// toggle the value
toggleValue();
}
Define a callback with a timeout
import React from 'react';
import { useTimeout } from '@avidian/hooks';
export default function Component() {
const { reset, clear } = useTimeout(() => {
// do something
}, 1000)
}
Define a callback with a debounce.
import React from 'react';
import { useDebounce } from '@avidian/hooks';
export default function Component() {
useDebounce(() => {
// do something
}, 5000, []);
}
Same as useEffect
but executes after update.
import React from 'react';
import { useUpdateEffect } from '@avidian/hooks';
export default function Component() {
useUpdateEffect(() => {
// do something after render
}, []);
}
Stores the previous value when updated.
import React, { useState } from 'react';
import { usePrevious } from '@avidian/hooks';
export default function Component() {
const [count, setCount] = useState(0);
const value = usePrevious(count);
// `value` will be 0 after `count` is updated to `1`
setCount(1);
}
Define state that maps to a storage.
remove
sets the value to undefined
.
import React from 'react';
import { useLocalStorage, useSessionStorage, useStorage } from '@avidian/hooks';
export default function Component() {
const [value, setValue, remove] = useLocalStorage('name', 'Joe');
const [value, setValue, remove] = useSessionStorage('name', 'Joe');
// custom storage
const storage = new ClassThatImplementsStorageInterface();
const [value, setValue, remove] = useStorage('name', 'Joe', storage);
}
This library is open-sourced software licensed under the MIT license.
FAQs
React helper hooks for convenience
The npm package @avidian/hooks receives a total of 0 weekly downloads. As such, @avidian/hooks popularity was classified as not popular.
We found that @avidian/hooks 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.