ios - React Native 中输入文本的绑定(bind)值不允许我再写了

标签 ios reactjs input react-native

我在子组件上获得了以下输入文本:

 <Input 
     placeholder={ i18n.t('searchSomeone') }
     value={ this.props.searchText }
     onChangeText={ this.props.onSearch }
     defaultValue={''}/>

这是方法,我正在传递变量和 onChangeText 处理程序:

<ChildComponent 
         onSearch={ this._onSearch }
         searchText={ this.state.searchText }>
</ChildComponent>

这是 _onSearch() 函数:

componentWillMount: function() {

        this._onSearch = _.debounce((text) => {
            this.setState({ refreshing : true }, () => {
                if(text.length === 0) {
                    this.setState({ filteredSearch: false }, () => {
                        this.getAllReports();
                    })
                }else{
                    this.setState({
                        page: 1,
                        refreshing: true,
                        reports: [],
                        filteredSearch: true
                    }, () => {
                        this.getReportsBySearchString();
                    })
                }
            })
        }, 1000);

    },

我想绑定(bind)这个输入文本值,因为当我执行上拉刷新时,我只想将文本设置为空字符串。

_onRefresh: function() {
    this.setState({
        filteredSearch: false,
        searchText: ''
    }, () => {
        this.getAllReports();
    })
},

但问题是,使用此实现,每当我尝试输入文本输入时,它都不会输入任何内容。

我错过了什么?

最佳答案

看起来您没有在 this.state.searchText 中保存输入值。这就是输入值始终为 '' 的原因。

this._onSearch = _.debounce((text) => {
        this.setState({ 
            refreshing : true,
            searchText: text // <-- Here
        }, () => {
            if(text.length === 0) {
                this.setState({ filteredSearch: false }, () => {
                    this.getAllReports();
                })
            }else{
                this.setState({
                    page: 1,
                    refreshing: true,
                    reports: [],
                    filteredSearch: true
                }, () => {
                    this.getReportsBySearchString();
                })
            }
        })
}, 1000);

编辑

您可以尝试去抖动您传递给 setState 的回调函数,它没有经过测试,所以我不确定它是否会起作用。

应该是这样的

this._onSearch = (text) => {
            this.setState({ 
                refreshing : true,
                searchText: text
            }, this._callback)

this._callback = _.debounce(() => {
    const text = this.state.searchText;
    if(text.length === 0) {
                  ...
    }
}, 1000);

关于ios - React Native 中输入文本的绑定(bind)值不允许我再写了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44079056/

相关文章:

ios - 包含不受支持的权利值

reactjs - React-native-maps 限制平移区域

java - 循环遍历tiles,只有get才能获取第一个tile的索引

c++ - 重载插入 >> 运算符?

javascript - 如何使用输入字段将值插入数组?

ios - 从 PHAsset 加载图像非常慢

ios - 当网络状态从蜂窝网络变为 wifi 时如何重新连接 VPN?

ios - 如何让音量键对 Audioplyer Sound 不显示效果

javascript - React/Redux 组件不重新渲染

javascript - React Component Array Map - 渲染没有返回任何内容。这通常意味着缺少返回语句。或者,不渲染任何内容,返回 null