Nicolas Silva
Nicolas Silva

Reputation: 639

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.

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

And this is the whole 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,
    });
  };

Upvotes: 2

Views: 220

Answers (1)

royalbhati
royalbhati

Reputation: 306

You can use useState hook instead of 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>
    }

Upvotes: 2

Related Questions