DEV Community

Discussion on: Quick Introduction to React Custom Hooks With Dropdown Selection

Collapse
 
vunderkind profile image
mogwai

Hey Mejanhaque!

When you fetch category[0], what do you get as a result? Let me know and I may be able to debug.

[As an aside: the standard pattern for setting categories would be not to add 'uncategorized' to the categories array, but to create a category hook you can reuse for different post components, with a ternary that returns 'Uncategorized' whenever the categories.length test is < 1.

Collapse
 
mejanhaque profile image
Muhammad Mejanul Haque

Thanks for mentioning the standard pattern. should the category value need to be null for fetching the uncategorized posts? and then should i try the categories.length method? I did set the category schema required true in mongoose & express.

when i fetch the category[0].title, it says title is undefined. I am using react hooks. other function worked perfectly. Thanks again for replying.

Thread Thread
 
vunderkind profile image
mogwai

Ah, seen!

Let's try debugging: Instead of fetching category[0].title, see if you can fetch category and either plant a debugger or log category to the console. We need to make sure the fetch is happening correctly, then check to see if the title key is present in the returned array object.

I suspect the problem is from the fetch in your hook, instead of in the assignment syntax afterward. Let me know what is returned from your API when you debug.