I want to know how I can make my “page” variable in the below code dynamic, so I can properly compare it with the location.pathname
variable which will have dynamic page ids.
import { useLocation } from 'react-router-dom';
function AddToCart() {
function IsItemPage() {
const page = '/item_page';
const location = useLocation();
console.log(location);
if (location.pathname == page) {
return true;
};
}
return IsItemPage() && (
<span className='add-to-cart'>
<button>Add Item</button>
<button>Cart</button>
</span>
);
}
export default AddToCart;
Now each item I open from the home page has a unique id for example: "/item_page/2"
and obviously I cannot hard code it. If I provide the absolute path to the “page” variable like "/item_page/1"
or something, the component is working as expected but only for that particular item with ‘id: 1’. But I need to be able to achieve this for every item I open from the home page.
So how can I achieve this?