1.0.9 • Published 4 years ago

use-controlled-effect v1.0.9

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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);

}
1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago