我正在尝试根据键盘是否打开来显示/隐藏组件。我正在努力理解为什么我最终会得到一个永无止境的循环。这是我的代码:
const [isKeyboardOpen, setIsKeyboardOpen] = useState(true);
useEffect(() => {
Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
Keyboard.addListener("keyboardDidHide", _keyboardDidHide);
// cleanup function
return () => {
console.log('cleanup')
Keyboard.removeListener("keyboardDidShow", _keyboardDidShow);
Keyboard.removeListener("keyboardDidHide", _keyboardDidHide);
};
}, []);
const _keyboardDidShow = () => {
console.log('opened')
setIsKeyboardOpen(true);
};
const _keyboardDidHide = () => {
console.log('closed')
setIsKeyboardOpen(false);
};
请帮忙。
我希望这可以解决您的问题
const [isKeyboardOpen, setIsKeyboardOpen] = useState(true);
useEffect(() => {
Keyboard.addListener("keyboardDidShow", () =>_keyboardDidShow());
Keyboard.addListener("keyboardDidHide", () =>_keyboardDidHide());
// cleanup function
return () => {
console.log('cleanup')
Keyboard.removeListener("keyboardDidShow",() => _keyboardDidShow());
Keyboard.removeListener("keyboardDidHide", () =>_keyboardDidHide());
};
}, []);
const _keyboardDidShow = () => {
console.log('opened')
setIsKeyboardOpen(true);
};
const _keyboardDidHide = () => {
console.log('closed')
setIsKeyboardOpen(false);
};