informed
Version:
A lightweight framework and utility for building powerful forms in React applications
30 lines (26 loc) • 713 B
JavaScript
import { useContext, useCallback } from 'react';
import { ScopeContext } from '../Context.js';
/* ----------------------- useScoper ----------------------- */
// This hook will return a scoper function!
function useScoper() {
var scope = useContext(ScopeContext);
var scoper = useCallback(function (name) {
// Example
// scope = "friends[0]"
// name = "friends[0]"
// return "friends[0]"
if (scope === name) {
return name;
}
// Example
// scope = "friends[0]"
// name = "name"
// return "friends[0].name"
if (scope && name) {
return "".concat(scope, ".").concat(name);
}
return name;
}, [scope]);
return scoper;
}
export { useScoper };