I Am trying to figure out how can I add the items to watchlist,
The steps am trying to carry out here are, when a user clicks on add button, the items should be added to the watchlist page/component which I have created.
Please see the hierarchy of the component. I would like to show added items on the watchlist page.
When I try to inspect, the result is, it shows the items are added on the console as well into the state but can not pass on to the watchlist component? How can I use a prop to pass this value and show them in the watchlist component? or is its props not the right way to pass any object into a component?
Hi,
I couldn’t find the snippet you posted here in your full code.
Generally speaking you just pass function references as props and call them in the child component on an html element: onClick = {handleWatchlist}
Why did you write this like a child component with capital letter? It just seems to be a regular button.
I haven’t understood your structure, yet: Which is the parent component and which is the child component that should receive the props?
Thanks for the reply, the button which you saying is a component of material UI hope this might help you to understand the component details, please refer Material Icons - Material-UI for more details on material UI.
Also apologize if you can not find the full code there.
I saw your component – that’s the code I posted. But it is not rendered anywhere yet.
And there aren’t any props passed to that component either.
You’re just calling a function that sets the state. But that state never gets anywhere…
Ok I found the handleWatchlist function now. Yet still the same problem: Where is the code rendered/injected? handleWatchlist is just a function returning a component – that from what I see never gets rendered.
That’s the same point, how can I pass or show the data into watchlist component such that when user clicks it updates the watchlist with that selected Item/Movie
or put it directly in your return statement.
I don’t see the point in having it in the handleClick function. That function should just update the state.
Please post the relevant and refactored code here, there is a lot of code in the code sandbox that is irrelevant for this and it’s a bit laborious to work my way through it.
Please post the code of two components without the imports and all that – with a headline which component is which:
The component with the function to update the state and the component that should receive the updated state and show the images (watchlist) and the part where Watchlist is returned in the return statement of the component rather than in a variable or function.
Thanks for the support, but I figured out the solution by using two approaches and they are
Which is props drilling, i.e. is to perform the useState action in my App.js which is my main page where I have product and watchlist component.
This is the best way I would suggest other people use as well is by using the contextAPI and useContext hooks . A simple and easy way to pass any data anywhere in the app.
Please see the full code with the working solution below on the codesandbox.
The working app, where I can add the products to the watchlist.