由状态反应本机引起的无限循环



我正在尝试根据键盘是否打开来显示/隐藏组件。我正在努力理解为什么我最终会得到一个永无止境的循环。这是我的代码:

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);
};

相关内容

  • 没有找到相关文章

最新更新