这个setState的react钩子等效于什么?

时间:2020-05-27 02:08:30

标签: javascript react-native react-hooks

我在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,
    });
  };

1 个答案:

答案 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>
    }