To achieve the desired effect, manipulating indexes of elements is crucial.
Rather than simply displaying content, it's necessary to determine the indexes of the element being hovered over:
const [displayIndex, setDisplayIndex] = useState({
typeIndex: -1,
colorIndex: -1
});
The event functions may be structured as follows:
const showButton = (typeIndex, colorIndex) => {
// e.preventDefault();
setDisplayIndex({
typeIndex,
colorIndex
});
};
const hideButton = () => {
// e.preventDefault();
setDisplayIndex({
typeIndex: -1,
colorIndex: -1
});
};
In order to conditionally render the button based on the displayIndex:
{
displayIndex.typeIndex === index
&&
displayIndex.colorIndex === i
&&
(<Button ....>Add</Button>)
}
If you implement these changes in your sandbox environment at https://codesandbox.io/s/add-to-cart-sampled-2-forked-9oi1kn?file=/src/App.js, you may need to adjust some styling aspects.
I trust you will find this information valuable.