Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Latest commit

 

History

History
54 lines (49 loc) · 1.47 KB

use-queue.hook.md

File metadata and controls

54 lines (49 loc) · 1.47 KB

useQueue

This React hook propose an implementation Queue data structure in FIFO method.
It's really useful when you need to queue data for excuting commands, or interpretings user inputs after they made them.

How to use it?

import { useQueue } from '@sugardarius/react-use-algos';

export function Commands() {
    const {
        add,
        remove,
        first,
        last,
        isEmpty,
        size,
        toArray,
    } = useQueue([
        'firstCommand',
        'secondCommand',
    ]);

    return (
        <div>
            <div>
                <span>{first()}</span>
                <span>{last()}</span>
                <span>{isEmpty()}</span>
                <span>{size()}</span>
            </div>
            <button
                onClick={() => {
                    add('thirdCommand')
                }}
            >
                Add third command
            </button>
            <button
                onClick={() => {
                    remove()
                }}
            >
                Remove
            </button>
            <pre>
                {JSON.stringify(toArray(), null, 2)}
            </pre>
        </div>
    );
}

Note: this hooks is based on react-use useQueue hook.