【发布时间】:2025-12-21 16:45:10
【问题描述】:
我在我的应用程序中使用 React-Navigation,该应用程序由具有多个屏幕的 StackNavigator 组成,其中一些屏幕具有带有 autoFocus={true} 的 TextInput
问题:在这些屏幕上,当组件渲染时,屏幕的高度是在构造函数中设置的:
constructor(props) {
super(props);
this.state = {
height: Dimensions.get('window').height,
};
}
但是,由于 TextInput 的 autoFocus 是 true,因此该屏幕上的键盘几乎在渲染后立即弹出屏幕上,导致组件重新渲染由于在 componentWillMount 中添加到 Keyboard 的 eventListener:
componentWillMount() {
this.keyboardWillShowListener = Keyboard.addListener(
"keyboardWillShow",
this.keyboardWillShow.bind(this)
);
}
keyboardWillShow(e) {
this.setState({
height:
Dimensions.get("window").height * 0.9 - e.endCoordinates.height
});
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
}
这会影响性能,我想避免不必要的重新渲染。
问题:
1. React-Navigation 的 ScreenProps 中是否可以设置键盘的动态高度(取决于设备)?
2. 是否可以对 React-Navigation 的 state.params 做同样的事情?
3. 除了使用KeyboardAvoidingView 或this module 之外,还有其他方法可以解决这个问题吗?
【问题讨论】:
标签: javascript react-native keyboard react-navigation