简体   繁体   English

如何在 React Native 的可搜索下拉列表中的 onTextChange 之后更新项目 object?

[英]how to update items object after on onTextChange in searchable dropdown in react native?

I am using react-native-searchable-dropdown for my react native project.我正在为我的 React Native 项目使用react-native-searchable-dropdown

When onTextChange is fired I make a call to my backend with the text the user inputs.onTextChange被触发时,我使用用户输入的文本调用我的后端。 Then I set the data I get back (array of objects) in the state:然后我在 state 中设置我返回的数据(对象数组):

 async inputBrandText (text) { const brands = await SearchForBrands(params); this.setState((state) => { return state.brands = brands }) };

In the items element I reference this state array:在 items 元素中,我引用了这个 state 数组:

 <SearchableDropdown textInputProps={ { placeholder: "", underlineColorAndroid: "transparent", style: {... }, onTextChange: text => this.inputBrandText(text) } } items={this.state.brands} />

But the items get refreshed one key stroke after it should actually be refreshed.但是这些项目在实际应该刷新之后会被一键刷新。 So I think when the onTextChange event is fired, the items object or array is read in. Then I change the this.state.brands object with the data from the backend.所以我认为当触发 onTextChange 事件时,项目 object 或数组被读入。然后我用后端的数据更改 this.state.brands object。 But then no update occurs and the items object is not updated.但是随后没有更新发生并且项目 object 没有更新。 Therefor the old items object is shown.因此显示了旧项目 object。

How can I solve this problem in a class based component?我如何在基于 class 的组件中解决这个问题?

Thanks!谢谢!

PS: This is my ComponendDidMount : PS:这是我的ComponendDidMount

 async componentDidMount () { const brands = await SearchForBrands(params); this.setState((state) => { return state.showBrandSuggestions = brands }) }

Try this way试试这个方法

Your componentDidMount method should be like你的componentDidMount方法应该是这样的

async componentDidMount () {
  const brands = await SearchForBrands(params);
  this.setState({ showBrandSuggestions: brands });
}

Your inputBrandText method should be like你的inputBrandText方法应该是这样的

async inputBrandText (text) {
  const brands = await SearchForBrands(params);
  this.setState({ brands: brands });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM