adrianhajdin / project_mern_memories

This is a code repository for the corresponding video tutorial. Using React, Node.js, Express & MongoDB you'll learn how to build a Full Stack MERN Application - from start to finish. The App is called "Memories" and it is a simple social media app that allows users to post interesting events that happened in their lives.
https://youtube.com/playlist?list=PL6QREj8te1P7VSwhrMf3D3Xt4V6_SRkhu
4.96k stars 1.83k forks source link

Preventing seeing no posts when clicking on the "Search" button while search inputs are empty #114

Open MatinT-SA opened 2 years ago

MatinT-SA commented 2 years ago

I realized that when you click on the "Search" button and both "tags" & "search" inputs are empty, then it's going to show you the loading icon and return an empty array which is not the expected output. This means it's still going to run the if block of code (if you pay attention to the url) while the else block must be executed instead of that.

It means we should change our conditions and just add length at the end of the search.trim() & tags so as to count the number of items in our array. This will produce the ideal and expected output and it will run the else block while we have written nothing in the search inputs and have also clicked on the search button instead of seeing no posts on the screen. So, it means it's going to redirect the user to the home page (history.push('/')) if we only click on the "Search" button without writing anything in its inputs.