UNPKG

react-barcode-scanner

Version:

A barcode scanner base on Barcode Detector

24 lines (23 loc) 663 B
import { useEffect, useState } from 'react'; export function createAtom() { var subscriptions = []; function set(newValue) { setTimeout(function () { subscriptions.forEach(function (c) { c(newValue); }); }); } return { set: set, subscriptions: subscriptions }; } export function useAtom(atom, initialState) { var _a = useState(initialState), state = _a[0], setState = _a[1]; useEffect(function () { var index = atom.subscriptions.push(setState); return function () { atom.subscriptions.splice(index, 1); }; }, [atom]); return [state, atom.set]; }