DEV Community

Adeyemi Raji
Adeyemi Raji

Posted on

How to build a todo app with search functionality with React with CSS

To build a todo app with search functionality using React and CSS, you can follow these steps:

  1. Create a new React app using the create-react-app command. npx create-react-app my-todo-app
  2. In the project directory, create a new component called TodoList that will render a list of todo items. You can use the state object to store the list of todo items and a handleChange function to update the list when a new todo item is added or an existing item is deleted.
import React, { useState } from 'react';

function TodoList() {
  const [todos, setTodos] = useState([]);

  const handleChange = (event) => {
    // code to add or delete todo items goes here
  }

  return (
    <div>
      <form>
        <input type="text" onChange={handleChange} />
        <button type="submit">Add Todo</button>
      </form>
      <ul>
        {todos.map((todo) => (
          <li key={todo.id}>{todo.text}</li>
        ))}
      </ul>
    </div>
  );
}

export default TodoList;

Enter fullscreen mode Exit fullscreen mode
  1. In the handleChange function, you can use the event.target.value to get the value of the input field and add it to the list of todos using the setTodos function. You can also implement a delete button for each todo item that will remove it from the list when clicked.
const handleChange = (event) => {
  const text = event.target.value;
  if (event.key === 'Enter' && text.trim()) {
    setTodos([...todos, { id: Date.now(), text }]);
    event.target.value = '';
  }
}

...

<button type="button" onClick={() => setTodos(todos.filter((t) => t.id !== todo.id))}>
  Delete
</button>

Enter fullscreen mode Exit fullscreen mode
  1. To add search functionality, you can create a new component called Search that will render an input field and a search button. The search button should call a function that filters the list of todos based on the search term.
import React, { useState } from 'react';

function Search() {
  const [searchTerm, setSearchTerm] = useState('');

  const handleSearch = () => {
    // code to filter the list of todos goes here
  }

  return (
    <div>
      <input type="text" onChange={(event) => setSearchTerm(event.target.value)} />
      <button type="button" onClick={handleSearch}>Search</button>
    </div>
  );
}

export default Search;

Enter fullscreen mode Exit fullscreen mode
  1. In the handleSearch function, you can use the searchTerm to filter the list of todos and update the state using the setTodos function. const handleSearch = () => { set todos(todos.filter((todo) => todo.text.toLowerCase().includes(searchTerm.toLowerCase()))); }
  2. To style the todo app, you can use CSS. You can either include the CSS in the component file itself using the style attribute or create a separate CSS file and import it into the component.

Here's an example of how you can style the TodoList component:

import './TodoList.css';

...

<div className="todo-list">
  <form>
    <input type="text" onChange={handleChange} />
    <button type="submit">Add Todo</button>
  </form>
  <ul>
    {todos.map((todo) => (
      <li key={todo.id} className="todo-item">
        {todo.text}
        <button type="button" onClick={() => setTodos(todos.filter((t) => t.id !== todo.id))}>
          Delete
        </button>
      </li>
    ))}
  </ul>
</div>
Enter fullscreen mode Exit fullscreen mode
.todo-list {
  display: flex;
  flex-direction: column;
}

.todo-item {
  display: flex;
  align-items: center;
}

.todo-item button {
  margin-left: 10px;
}

Enter fullscreen mode Exit fullscreen mode
  1. Finally, you can use the TodoList and Search components in the main App component to render the todo app.
import React from 'react';
import TodoList from './TodoList';
import Search from './Search';

function App() {
  return (
    <div>
      <Search />
      <TodoList />
    </div>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

I hope this helps! Let me know if you have any questions.

Top comments (0)