O
O
Orion Orion2022-01-23 15:32:53
JavaScript
Orion Orion, 2022-01-23 15:32:53

Update in react?

const { logout } = useAuth();
    const [tasks,setTasks] = useState([]);
    const { id } = JSON.parse(localStorage.getItem('id'));
    const [search,setSearch] = useState(null);

    const changeSearch = (e) => {
        setSearch(e.target.value)
    }

    const reviewTasks = async () => {
        await axios.get(`http://localhost:5000/task/`, {params: {id}}).then(response => {
                setTasks(response.data)
            })
    }
    const filterTask = tasks.filter(({task}) => {
        return task.includes(search)
    })

        useEffect( async () => {
            reviewTasks()
        },[])

    console.log(filterTask)
  return <div>
  
        {tasks&&
                filterTask.map((task) => (
                    <div key={task.id} className="task">
                        <p className='name'>{task.task}</p>
                        <Delete className='delete'/>
                    </div>
                ))
                }
            <div className="search-div">
            <h3>Search</h3>
                <form onSubmit={e => e.preventDefault()}>
                    <input className='search-inp' type="text" placeholder='Search a Task' onChange={changeSearch}/>
                </form>
            </div>
  </div>;
}

I do a search, but as a result it does not display tasks, everything works fine after I drive something in the input
, this is before entering something in the input
61ed4ad0e4b3e091563186.png

, after that it is 61ed4af0aaf6b345587659.png

necessary that all tasks be shown at once!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-01-23
@Harlock

const [search,setSearch] = useState(null);

change the starting value, otherwise, before replacing this state, task.includes(null) is done in the task filter and returns false, because in this function the argument is cast to a string, null turns into 'null'
const [search,setSearch] = useState('');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question