Skip to content

Commit

Permalink
feat: add useChangeable hook
Browse files Browse the repository at this point in the history
  • Loading branch information
welingtonms committed Feb 22, 2021
1 parent 914049e commit fc99b5a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/hooks/changeable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useChangeable } from './use-changeable'
20 changes: 20 additions & 0 deletions src/hooks/changeable/use-changeable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback, useState } from 'react';

function useChangeable({ onChange: onChangeProp, value: valueProp }) {
const [value, setValue] = useState(valueProp);

const onChange = useCallback(
function handleChange(e) {
const {
target: { value },
} = e;
setValue(value);
onChangeProp?.(e);
},
[onChangeProp],
);

return [value, onChange];
}

export default useChangeable;

0 comments on commit fc99b5a

Please sign in to comment.