React Colour Filter Application

JavaScriptJavaScriptIntermediate
Practice Now

Introduction

In this project, you will learn how to build a colour filter application using React. The application will allow users to filter a list of colours by typing in the name of the colour they are looking for. This project will help you understand how to work with state management, event handling, and conditional rendering in React.

👀 Preview

project preview

🎯 Tasks

In this project, you will learn:

  • How to set up a React project and manage dependencies
  • How to implement a real-time colour filtering functionality
  • How to style the application using CSS

🏆 Achievements

After completing this project, you will be able to:

  • Set up a React project and install dependencies
  • Utilize state management in React to filter data
  • Handle user input events and update the UI accordingly
  • Style a React application using CSS

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) react(("`React`")) -.-> react/FundamentalsGroup(["`Fundamentals`"]) react(("`React`")) -.-> react/AdvancedConceptsGroup(["`Advanced Concepts`"]) react(("`React`")) -.-> react/StateManagementGroup(["`State Management`"]) react(("`React`")) -.-> react/StylingGroup(["`Styling`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/es6("`ES6 Features`") react/FundamentalsGroup -.-> react/jsx("`JSX`") react/FundamentalsGroup -.-> react/event_handling("`Handling Events`") react/FundamentalsGroup -.-> react/list_keys("`Lists and Keys`") react/AdvancedConceptsGroup -.-> react/hooks("`React Hooks`") react/StateManagementGroup -.-> react/use_state_reducer("`Using useState and useReducer`") react/StylingGroup -.-> react/css_in_react("`CSS in React`") subgraph Lab Skills javascript/variables -.-> lab-300140{{"`React Colour Filter Application`"}} javascript/data_types -.-> lab-300140{{"`React Colour Filter Application`"}} javascript/arith_ops -.-> lab-300140{{"`React Colour Filter Application`"}} javascript/comp_ops -.-> lab-300140{{"`React Colour Filter Application`"}} javascript/cond_stmts -.-> lab-300140{{"`React Colour Filter Application`"}} javascript/higher_funcs -.-> lab-300140{{"`React Colour Filter Application`"}} javascript/es6 -.-> lab-300140{{"`React Colour Filter Application`"}} react/jsx -.-> lab-300140{{"`React Colour Filter Application`"}} react/event_handling -.-> lab-300140{{"`React Colour Filter Application`"}} react/list_keys -.-> lab-300140{{"`React Colour Filter Application`"}} react/hooks -.-> lab-300140{{"`React Colour Filter Application`"}} react/use_state_reducer -.-> lab-300140{{"`React Colour Filter Application`"}} react/css_in_react -.-> lab-300140{{"`React Colour Filter Application`"}} end

Set Up the Project

In this step, you will learn how to set up the project and install the necessary dependencies.

Open your code editor and navigate to the project directory.

├── public
├── src
│   ├── App.css
│   ├── App.js
│   ├── index.css
│   ├── index.js
│   ├── reportWebVitals.js
│   └── setupTests.js
├── package-lock.json
└── package.json

Next, download the dependencies using the npm install command in the terminal, wait for the dependencies to finish downloading and then start the project using the npm start command.

Once the project starts successfully, click on the "Web 8080" to preview it in your browser.

Implement the Colour Filter

In this step, you will implement the colour filter functionality.

  1. Open the App.js file in your code editor.

  2. In this file, import the necessary dependencies:

    import React, { useState } from "react";
  3. Add a state variable inside const App = () => {} to store the filtered list of colors:

    const [filteredList, setFilteredList] = useState(colors);
  4. Implement the handleOnChange function to filter the colour list based on the user's input:

    const handleOnChange = (e) => {
      const value = e.target.value;
      if (!value) {
        setFilteredList(colors);
      }
    
      const filtered = colors.filter((c) =>
        c.name.toUpperCase().includes(value.toUpperCase())
      );
      setFilteredList(filtered);
    };
  5. Render the input field and the filtered list of colours:

    return (
      <div className="app">
        <input
          className="filter-input"
          type="text"
          name="filter"
          placeholder="Enter a colour name to see filtered results"
          onChange={handleOnChange}
        />
        <div className="list">
          {filteredList.map((c) => {
            return (
              <div
                className="list-item"
                key={c.name}
                style={{ backgroundColor: c.hex }}
              >
                {c.name}
              </div>
            );
          })}
        </div>
      </div>
    );
  6. The complete code within const App = () => {} is as follows:

const App = () => {
  const [filteredList, setFilteredList] = useState(colors);

  const handleOnChange = (e) => {
    const value = e.target.value;
    if (!value) {
      setFilteredList(colors);
    }

    const filtered = colors.filter((c) =>
      c.name.toUpperCase().includes(value.toUpperCase())
    );
    setFilteredList(filtered);
  };

  return (
    <div className="app">
      <input
        className="filter-input"
        type="text"
        name="filter"
        placeholder="Enter a colour name to see filtered results"
        onChange={handleOnChange}
      />
      <div className="list">
        {filteredList.map((c) => {
          return (
            <div
              className="list-item"
              key={c.name}
              style={{ backgroundColor: c.hex }}
            >
              {c.name}
            </div>
          );
        })}
      </div>
    </div>
  );
};
  1. Save the App.js file, and the application should now filter the colour list in real-time as the user types in the input field.
project preview

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like