How to retrieve data upon state change?
Answered
Brown bear posted this in #help-forum
Brown bearOP
I'm using NextJS 14, and I want to set up something like this:
I have two sections on my page. The left one lists products, and when a product is selected, I want the right section to show detailed info about that product. How can I fetch this detailed information from a database when a product is selected?
What are some ways to achieve this?
I have two sections on my page. The left one lists products, and when a product is selected, I want the right section to show detailed info about that product. How can I fetch this detailed information from a database when a product is selected?
What are some ways to achieve this?
9 Replies
And then setState
Right section uses state to find out the product and display it
@Anay-208 Right section uses state to find out the product and display it
Brown bearOP
Should I make the db call from the right component?
@Brown bear Should I make the db call from the right component?
Create a separate api route for that
And I think you should already have the product details on left section, just use it
Is your query resolved @Brown bear
@Anay-208 Create a separate api route for that
Brown bearOP
I only fetch some metadata to render the left section like product ID and product name, once the user selects something, I want to make a DB call to get more details of the selected product. I'm assuming you are saying, I need to add an API route handler and from there I should make the API call. And then in the right section, make a fetch call in useEffect?