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 but can not pass on to the watchlist component? How can use a prop to pass this value and show them?
This is how my watchlist component looks like. I think I might have to use map method to show added data, but am not sure is it the right way am thinking. Please correct me sir.
Its hard for me to figure out where is the button which modify the watchlist state and where is the watchlist component? Are they parent and child elements, or do they share the same parent?
Hi @ajayraja
Thanks a million for the help but I have figured about the solution to this, @Sylvant thanks for the support yeah they share the same parent, as the are inside the app.js which is main component.
However, 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.
this is what i was about to suggest(once i knew in what sense the two elements are connected). You can either store the state in the parent component and pass to the button the function to modify the state, while pass to the watchlist the state value. Or the other solution to use context to store the app state and give access to components which use it. Ofc both solutions would make components involved in the state change to be rerendered, but this is something you shouldnt worry for smaller apps