use-controlled-effect
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

useControlledEffect React Hook

Parts of this code is based on use-debounced-effect

You can install it using

npm i use-controlled-effect

Dummy TS code example:

import useControlledEffect from 'use-controlled-effect';
import api from '...';

interface IMyComponentProps {
    someId: number;
}

const MyComponent: React.FC<IMyComponentProps> = (props) => {

    // returns true if effect should run immediately 
    const loadNow = someCondition ? true : false;

    // async side effect that loads data from an api
    const loadData = useCallback(() => {
        const fetchData = async () => {
            const data = await api.loadData(props.someId);
            //...
            setLoading(false);
        };

        setLoading(true);
        fetchData();
    }, [props.someId]);

    useControlledEffect(loadData, () => loadNow, [props], 100);

}

Package Sidebar

Install

npm i use-controlled-effect

Weekly Downloads

4

Version

1.0.9

License

MIT

Unpacked Size

4.05 kB

Total Files

4

Last publish

Collaborators

  • aesser