获得徽章 0
- 单个 Input 没必要用 form 包裹
```ts
const useEnterCallback = (callback) => {
const inputRef = useRef(null);
useEffect(() => {
const handleKeyPress = (event) => {
if (event.key === 'Enter') {
callback(inputRef.current.value);
}
};
inputRef.current.addEventListener('keydown', handleKeyPress);
return () => {
inputRef.current.removeEventListener('keydown', handleKeyPress);
};
}, [callback]);
return inputRef;
};
```展开等人赞过评论5
![[阴险]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_49.392c709.png)
![[奸笑]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_17.bcebf79.png)
![[流泪]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_6.dde0d83.png)