【问题标题】:onSubmitEditing: Error: Maximum update depth exceededonSubmitEditing:错误:超出最大更新深度
【发布时间】:2025-11-27 13:20:08
【问题描述】:

我的屏幕上有一个文本输入。除此之外,还有一个搜索图标。按下搜索图标后,将调用 this.searchmethod() 并执行搜索。我还想在按下键盘的“搜索”、“完成”、“开始”或“输入”按钮时执行搜索。我尝试了如下所示的 onSubmitEditing,导致错误:

错误:超过最大更新深度 我的代码:

<TextInput 
    onChangeText = {
    text => {
        this.setState({
            searchtag: text
        })
    }
}
    onSubmitEditing={this.searchmethod()}
    onFocus = {() => {
        if (this.state.searchtag != "")
            this.setState({
                searchtag: ''
            }, () => {
                this._refreshData()
            })
    }
}
/> 
<TouchableWithoutFeedback
    onPress = {
        () => {
            this.searchmethod()
        }
    } >
    <View>
        <FearIcon name = "search" size = {25}/> 
    </View> 
</TouchableWithoutFeedback>

请指导我如何在按键盘上的“完成”或 etc 键时执行搜索。提前致谢。

【问题讨论】:

    标签: react-native


    【解决方案1】:

    尝试这样做

    onSubmitEditing={() => this.searchmethod()}
    

    onChangeText 中的 refreshData 代替 onFocus 之类的

    this.setState({
                searchtag: text
            },
         () => this._refreshData();
    ) 
    

    【讨论】: