Search functionality in reactjs

share link

by vsasikalabe dot icon Updated: Dec 16, 2022

technology logo
technology logo

Solution Kit Solution Kit  

we import useState from react . Then, we import the Scroll and SearchList components. Next, in the Search function, we use the useState hook to initialise the value of the searchField state with useState("") (an empty string). After this, we use the filter function on the details list received from the parent.


Preview of the output that you will get on running this code from your IDE.


Code:

In this solution we use the React library.

let data = [
  {
    dataConnectionName: "Customer_Details",
    dataConnectionType: "NO_SQL",
    databaseHost: "17.8.10.26",
    pluginName: "AGT1_Customer_Details",
    createdDate: "2018-09-23",
    createBy: "Admin"
  },
  {
    dataConnectionName: "User_Details",
    dataConnectionType: "NO_SQL",
    databaseHost: "17.8.10.26",
    pluginName: "AGT1_Customer_Details",
    createdDate: "2018-09-24",
    createBy: "Admin"
  },
  {
    dataConnectionName: "Manager_Details",
    dataConnectionType: "NO_SQL",
    databaseHost: "17.8.10.26",
    pluginName: "AGT1_Customer_Details",
    createdDate: "2018-09-25",
    createBy: "Admin"
  },
  {
    dataConnectionName: "Director_Details",
    dataConnectionType: "NO_SQL",
    databaseHost: "17.8.10.26",
    pluginName: "AGT1_Customer_Details",
    createdDate: "2018-09-26",
    createBy: "Admin"
  }
];

// Give each element a unique id that is used as key
data.forEach(el => el.id = Math.random());

class App extends React.Component {
  state = {
    data,
    filteredData: data
  };

  _handleSearchChange = e => {
    const { value } = e.target;
    const lowercasedValue = value.toLowerCase();

    this.setState(prevState => {
      const filteredData = prevState.data.filter(el =>
        el.dataConnectionName.toLowerCase().includes(lowercasedValue)
      );

      return { filteredData };
    });
  };

  render() {
    const { filteredData } = this.state;

    return (
      <div>
        <input onChange={this._handleSearchChange} placeholder="Search"/>
        {filteredData.map(el => (
          <div key={el.key}>
            <div>
              {el.dataConnectionName} - {el.pluginName} - {el.createdDate} - {el.createBy}
            </div>
          </div>
        ))}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>
  1. Copy the code using the "Copy" button above, and paste it in Visual Stdio Code..
  2. Create React application using npx create-react-app foldername
  3. cd foldername.
  4. npm start to run the file.


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for Search functionality in reactjs in kandi. You can try any such use case!


Dependent Libraries:

create-react-appby facebook

JavaScript doticonstar image 100082 doticonVersion:v5.0.1doticon
License: Permissive (MIT)

Set up a modern web app by running one command.

Support
    Quality
      Security
        License
          Reuse

            create-react-appby facebook

            JavaScript doticon star image 100082 doticonVersion:v5.0.1doticon License: Permissive (MIT)

            Set up a modern web app by running one command.
            Support
              Quality
                Security
                  License
                    Reuse

                      Environment Tested:

                      I tested this solution in the following versions. Be mindful of changes when working with other versions.

                      1. The solution is created in VS Code 1.73.1 version.
                      2. The solution is tested on Nodejs 16.14.2 version.
                      3. react 18.2.0 version.


                      You can search for any dependent library on kandi like react.



                      Support:

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.


                      See similar Kits and Libraries