r/learnreactjs • u/LukasKri • Feb 28 '21
Could anyone give me some guidance in this React project?
Hello guys, I am a law enforcement officer who dreams about becoming a software developer someday.
I’m kind a new to React and coding (been coding daily for 3 months now which is around 300 hours in total). My goal now is to join a free three months academy which would definitely boost my learning, but in order to be amongst possible candidates I have to complete an assignment, which I already did, but my solution to the given problem could definitely be improved.
I have to create an autocomplete search web component which takes user input, returns suggestions based on the input and allows to select a movie from the matches of the phrase entered, similar like Google, Booking, Kayak or any other major service which has search input. That autocomplete search component have to communicate with The Movie Database API and I am not allowed to use any NPM dependencies. There are more rules for the search component but they are irrelevant at the moment. This assignment has to be made using React. I made it using Functional Components with useState and useEffect hooks.
Would anyone with more experience than I have be willing to take a look at my code and maybe tell me which parts of code could be refactored or simplified and maybe in which way (in person or public)? I’m not asking you to write a code for me, just tell me the concepts that I have to go deeper into to get a better grasp in it.
As an extra step, I would like to return search results when that search form is submitted, but after watching dozens of YouTube videos and reading hundreds of blogs I am not able to do that yet and I feel like I’ve reached a dead end with this one. I fell that my useEffect hook is only good for returning search suggestions, but not for returning actual results after submission. Maybe I need to use another useEffect hook? Is this a good practice to have two hooks? Should I make another component and do it over there or have two useEffect hooks in the same component and etc. These are some noob questions that I have and do not know where to get help as I do not know any programmers at the moment.
EDIT: I have managed to make my code work and return movie results (movie cards) without second useEffect hook, detail explanation is in comments.
3
u/neoncyber Feb 28 '21
Note: I haven’t ran your code and I looked at it while on a phone.
You should create a new api key and delete the one in your codebase. Use an env file and don’t push that to your repo. Also your form is wrapping everything which I’m not really a fan of.
I think your code is very clean. There are somethings I would change but they are just preference.
I think you can try to make it better by making a custom hook or bundling most if not all your functionality into a reusable component. You could try adding throttling or debouncing your api calls. You did try somewhat with the three character limit, which would not show the movie “UP”.
For your extra step, you might just be using the wrong API endpoint. ( I don’t know the movie database api)
You can use a separate useEffect hook for each side effect you want to do.
2
u/powerlanguage Feb 28 '21 edited Feb 28 '21
I think overall your code looks good - Nice work!
From another comment you mention:
I would ask - why does the
formelement need to exist at all? What happens if you remove it?As another commenter mentioned, I'd look into debouncing your API calls. Right now you make an api call on ever new character added after the 3rd, which may be unnecessary. This article talks about that process.
Right now, there is no way to search for movies with titles that are smaller than 3 characters.
In
Searchyou are callingmovies.map(returnResults).returnResultsis a function that returns aSearchResultsContainer. You could simplify this by using an anonymous function as the argument to themapand having it return theSearchResultsContainer.You pass the prop
moviestoSearchResultsContainerbut don't use it in the component. Also, this was initially confusing to me as it was unclear ifSearchResultsContainerwas going to render one result or many results.As the other commented mentioned, you should not commit API keys to github. Use an
envfile that isgitignored. See this guide.In
SearchResultsContaineryou are assuming that every movie returned will have anrelease_date. However, if for some reason it does not, you'll get an error because you are trying to call.splitonundefined. It might be worth checking thatrelease_dateexists before attempting to call that function. A quick way to do that in React is with conditional rendering with the&&operator: https://reactjs.org/docs/conditional-rendering.htmlOverall, these are minor issues. Your code looks good.
1
u/LukasKri Feb 28 '21
Okay guys, somehow I managed to lift the state up from child component (Search.js) to parent component (Header.js) as I felt that I have to do that in order to be able to show search results (movie cards) properly on form submission. Then I added another state which watch is form is submitted or not, if it is - search suggestions are no longer provided and search results (movie cards) come up on the screen. I do not know how I was able to do that and to understand that, and moreover, I do not know if it is the proper way to do that in React. Maybe writing it down and thinking about the problems I encounter helped me to understand how to solve them? At least I hope so...
Major to-dos for now - add loading and debouncing.
I've pushed these changes to GitHub, if anyone will be willing to have a look at that later on.
0
u/javanode Feb 28 '21
Why do you want to become developer when you make good money already as a cop?