Skip to content

Commit

Permalink
feat: wrap handleChange in useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
kleysc committed Jan 20, 2025
1 parent d5dcca2 commit 4c09558
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions apps/router/src/hooks/custom/useTrimmedInput.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { useState } from 'react';
import { useState, useCallback } from 'react';

const cleanInput = (value: string) => value.trim();
const cleanInput = (value: string | undefined) => value?.trim() ?? '';

export const useTrimmedInput = (initialValue = '') => {
const [value, setValue] = useState(initialValue);

const handleChange = useCallback((newValue: string) => {
setValue(cleanInput(newValue));
}, []);

return [value, handleChange] as const;
};

export const useTrimmedInputArray = (initialValues: string[]) => {
const [values, setValues] = useState<string[]>(initialValues);

const handleChange = (index: number, newValue: string) => {
const handleChange = useCallback((index: number, newValue: string) => {
setValues((prev) => {
const newValues = [...prev];
newValues[index] = cleanInput(newValue);
return newValues;
});
};
}, []);

return [values, handleChange] as const;
};

0 comments on commit 4c09558

Please sign in to comment.