简体   繁体   English

这个 setState 的反应钩子是什么?

[英]What's the react hooks equivalent of this setState?

I found this code in the react-native-dynamic-search-bar , and I couldn't find a way to make it with react-native-hooks .我在react-native-dynamic-search-bar中找到了这段代码,但我找不到使用react-native-hooks的方法。

this.setState({
  query: text,
  dataSource: newData,
});

And this is the whole function这是整个 function

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

You can use useState hook instead of setState您可以使用useState挂钩而不是 setState

and you can rewrite that like this:你可以这样重写:

  // 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>
    }

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

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