Working on a React app where you can search through an API and add favorites. I had the search working, but when I added the favorites method I am now getting a “TypeError: Cannot read property ‘map’ of null” error in my MovieList.js, pointing out my <React.Fragment> code.
When I console.log(props.movies); it comes back as “props is undefined”. I don’t get this, I had it working earlier and could search through movies. Is something incorrect with the code I added for the Favorites function?
You do zJSON.parsez for value by key from localStorage, but value may be empty and after JSON.parse(null) will be Error. Try try/catch or check value and do JSON.parse after valid value by key.
I still have the same error message with “cannot properly map of null”, but I am not seeing anything displayed using the try/catch method on the JSON local storage.
It happens because your props.movies in MovieList.js have undefined value. It will happen if you do setFavorites(undefined|null). I see your component’s code and see it problem place:
I see that you have a default value for favorites as empty array [], but you rewrite it in useEffect and your value will be different value, it value is null.
useEffect is supposed to save the setFavorites(movieFavorites) into an empty array. I still don’t understand how my props.movies is undefined. I don’t have my setFavorites as (undefined|null), I have it set as movieFavorites.
What exactly needs to be changed with my useEffect? I don’t get why it is having an effect on my MovieList component.
No, setFavorites(movieFavorites) not write value to empry value, it revrite your value [] to null. If you have not data by key app-favorites in page localStorage localStorage.getItem('app-favorites') will be null and JSON.parse(null) will be Error.
Use here try {} catch() {} for exeption and if (movieFavorites).
I run your code and resolved problem. Correct useEffect is:
Hi @Fork. It is actually not recommended to define logic for performing external effects outside useEffect. It is advisable to define it inside useEffect like.
I would also recommend you look at how you are updating searchValue. Are you sure you are not updating searchValue in onChange event handler. Try console logging the data fetched to see what the value is. I suspect you are triggering a call to the API on every key stroke. Unfortunately I wasn’t able to play with your code on codesandbox because it appears you forgot to save the code in some files so I get some errors.
I tried @lasjorg 's method originally but I got unexpected end of JSON input.
If I use @vladimirschneider if statement, my page shows up. I took @nibble 's advice and defined my code inside useEffect. However, when I search it crashes and gives me an “element type invalid” error for MovieList.js. When I console log searchValue it returns each key stroke. However I intended it to show the results while your searching so you don’t have to enter in your entire query and hit send, so it is supposed to log each keystroke.
The error is below, it is the same one I got earlier.
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it’s defined in, or you might have mixed up default and named imports. Check the render method of MovieList.
As long as you try to use empty components (like you have in the Codesandbox) you will get that error. Without seeing all the code, i.e. with all the components actually having code in them, we can’t really test your code.
Here it is with all the empty components commented out and two fixes (ignore the thumbnail for some reason it is not updating).
https for the fetch URL
Using || for localStorage.
Well, and a key for the map, but that’s about it. I can test the search because the component is empty.