我在react-native-dynamic-search-bar
中找到了此代码,但找不到用react-native-hooks
进行编码的方法。
this.setState({
query: text,
dataSource: newData,
});
这是整个功能
const filterProjectList = (text) => {
var newData = dataBackup.filter((item) => {
const itemData = item.name.toLowerCase();
const textData = text.toLowerCase();
return itemData.indexOf(textData) > -1;
});
this.setState({
query: text,
dataSource: newData,
});
};
答案 0 :(得分:2)
您可以使用 useState钩子代替setState
,您可以这样重写:
// yourComponent.js
import React, {useState} from 'react'
const yourComp =() =>{
const [query,setQuery] = useState("")
const [dataSource,setDatasource] = useState([])
const filterProjectList = (text) => {
var newData = dataBackup.filter((item) => {
const itemData = item.name.toLowerCase();
const textData = text.toLowerCase();
return itemData.indexOf(textData) > -1;
});
setQuery(text)
setDatasource(newData)
};
....
return <div>Hello World</div>
}