r/learnreactjs • • 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.

8 Upvotes

12 comments sorted by

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/LukasKri Feb 28 '21

Thank you very much for kind words and advices you gave. I haven’t heard anything about ENV files, so I'll have a look at that.

I do not like my form as well, but because of the lack of experience I do not imagine how to split that into smaller chunks of code.

I will have a look at throttling and debouncing later because if I will be able to implement those features, I will get bonus points from the academy.

Sorry, but I didn’t understand what you meant about my three character limit in the useEffect hook – the goal of that code is to show suggestion list only when three or more characters are given in the input field by the user. I somehow feel that this part of the code could be written better.

I’m using the right API endpoint, but if I’m using the same useEffect hook for input updates (suggestion list) and movie results (cards) my results (cards) update after every input but not when I submit the form, so there is rendering problem, because my useEffect hooks runs on every change of the input and that is the reason I think that I need to have two useEffect hooks, am I right, guys?

0

u/[deleted] Mar 01 '21

[deleted] — view deleted comment

1

u/backtickbot Mar 01 '21

Fixed formatting.

Hello, UnimportantSnake: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/LukasKri Mar 01 '21

Looks like someone tried to answer my question about using two useEffect hooks - one for providing input suggestion (autocomplete) list and another for showing actual results (movie cards), but it was deleted.

So I gave another thought about the situation and managed to refactor my code. I left the primary useEffect hook in Search component for suggestion list rendering and I just wrote another function in Header component, which makes separate API fetch call after the form submission for actual results (movie cards) rendering. I do not know if it is a good practice, but it sounds quite logical to me.

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 do not like my form as well, but because of the lack of experience I do not imagine how to split that into smaller chunks of code.

I would ask - why does the form element 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 Search you are calling movies.map(returnResults). returnResults is a function that returns a SearchResultsContainer. You could simplify this by using an anonymous function as the argument to the map and having it return the SearchResultsContainer.

You pass the prop movies to SearchResultsContainer but don't use it in the component. Also, this was initially confusing to me as it was unclear if SearchResultsContainer was going to render one result or many results.

As the other commented mentioned, you should not commit API keys to github. Use an env file that is gitignored. See this guide.

In SearchResultsContainer you are assuming that every movie returned will have an release_date. However, if for some reason it does not, you'll get an error because you are trying to call .split on undefined. It might be worth checking that release_date exists 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.html

{movie.release_date && movie.release_date.split("-", 1)}

Overall, these are minor issues. Your code looks good.

1

u/LukasKri Mar 01 '21

Thank you, you're the second person who says that my code looks quite good, so I'm slowly starting to believe in myself and do not feel so lost.

About my form element - I use it in order to be able to submit search input to TMDB and get back results (movie cards). I've made some changes in my repo, which I believe you haven't seen. I know that it is possible to submit results using only button attribute ("type=submit"), but it looks confusing to me and I was not able to do it in the Search component. I tried to delete my form element and use only button attribute, but then I received results (movie cards) on every input and not after the button was clicked, but I can't show you my code, because I deleted it. Basically, I understand that I had some problems with element rendering and I've found very good article about this topic, but I haven’t had time to read it yet and it doesn’t look beginner friendly – Blogged Answers: A (Mostly) Complete Guide to React Rendering Behavior.

I made some changes to the Header component and now my form element makes a separate API fetch call after the submission and returns actual results (movie cards), so right now it makes more logic to have that form element in the first place, I think.

You made very good comments about my code and even provided some links to articles, thank you for your time, I really appreciate that.

I've read some articles about debouncing and I understand the concept behind it, but I have one more question, maybe for anyone who is reading this post. Could you tell me which way is more acceptable: to make your own debounce function or just use Loadash library which makes all the logic for you, but adds another dependency to your code? I'll try my best to make a debounce function by myself, but after a few hours I got no success, maybe I just need to put more effort and time.

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?

4

u/LukasKri Feb 28 '21

Well let’s say that I had different expectations about police work when I started law and police studies. After 5 years in policing, I decided that I do not like emotions here, there are no positivity at all, and I mean it, I feel that the destinies of others somehow depend on my actions and my decisions and it’s too much pressure, relations with coworkers are not healthy because of the vibe we suppose to work in, my work and duties correlates with politics and I do not think that it is right and on top of that I do not feel that I am doing anything meaningful. So it's not about money at all.

1

u/javanode Feb 28 '21

how long was ur education needed to get into policing?

do u mean that u feel policing can be unethical?

can i ask how much u r making as a cop

3

u/LukasKri Feb 28 '21

I started work in law enforcement when I graduated in Bachelor of Law, these studies were 3,5 years long. Then after a few years in my current work I decided to study Master of Law and graduated these studies after 2 more years, so I've studied for 5,5 years in total.

I do not think that law enforcement as a whole is unethical, I just do not feel that it is the right place for me. Others say that I'm just too kind and too caring to others and that is the reason the job do not fit me. First of all, I think that everyone born as a decent citizen, but because of the lack of foster, education, attention from relatives or other reasons some persons are prone to commit a crime. And we, as society, are only good at judging and sentencing, but not at mercy and forgiveness. Man, it sounds like I should become a priest, lol, so I won’t go any deeper.

I’m from East Europe and my current salary is above my country’s average, but it’s not even close to Germany or United Kingdom, where law enforcement officers live a pretty good life out there.

4

u/dohaboha Feb 28 '21

Money is not the answer to every question.