MCPcopy
hub / github.com/DavidHDev/react-bits / useComponentProps

Function useComponentProps

src/hooks/useComponentProps.js:35–102  ·  view source on GitHub ↗
(defaultProps)

Source from the content-addressed store, hash-verified

33};
34
35export function useComponentProps(defaultProps) {
36 const defaultPropsRef = useRef(defaultProps);
37
38 const parsers = useMemo(() => {
39 const result = {};
40 for (const [key, defaultValue] of Object.entries(defaultPropsRef.current)) {
41 result[key] = createParser(defaultValue);
42 }
43 return result;
44 }, []);
45
46 const [queryState, setQueryState] = useQueryStates(parsers);
47
48 const props = useMemo(() => {
49 const merged = { ...defaultPropsRef.current };
50 for (const [key, value] of Object.entries(queryState)) {
51 if (value !== null) {
52 merged[key] = value;
53 }
54 }
55 return merged;
56 }, [queryState]);
57
58 const hasChanges = useMemo(() => {
59 return Object.values(queryState).some(v => v !== null);
60 }, [queryState]);
61
62 const updateProp = useCallback(
63 (name, value) => {
64 const newValue = value === defaultPropsRef.current[name] ? null : value;
65 setQueryState({ [name]: newValue });
66 },
67 [setQueryState]
68 );
69
70 const updateProps = useCallback(
71 updates => {
72 const newState = {};
73 for (const [name, value] of Object.entries(updates)) {
74 newState[name] = value === defaultPropsRef.current[name] ? null : value;
75 }
76 setQueryState(newState);
77 },
78 [setQueryState]
79 );
80
81 const resetProps = useCallback(() => {
82 const resetState = {};
83 for (const key of Object.keys(defaultPropsRef.current)) {
84 resetState[key] = null;
85 }
86 setQueryState(resetState);
87 }, [setQueryState]);
88
89 const getShareUrl = useCallback(() => {
90 return window.location.href;
91 }, []);
92

Callers 15

CircularTextDemoFunction · 0.85
CurvedLoopDemoFunction · 0.85
TextCursorDemoFunction · 0.85
ShuffleDemoFunction · 0.85
VariableProximityDemoFunction · 0.85
SplitTextDemoFunction · 0.85
BlurTextDemoFunction · 0.85
ScrollFloatDemoFunction · 0.85
GradientTextDemoFunction · 0.85
GlitchTextDemoFunction · 0.85
TrueFocusDemoFunction · 0.85
DecryptedTextDemoFunction · 0.85

Calls 1

createParserFunction · 0.85

Tested by

no test coverage detected