I am trying create a like button inside a post. ex) Shopping mall post where you click the post then goes to post detail page, but when you click button, you dont move page but mark only the like button.
import {isLoggedIn} from "recoil/AtomUser" // recoil is an alternative to redux
import {Link, useNavigate} from "react-router-dom"
const FashionItem = ({ItemNo}) => {
const navigate = useNavigate();
const [isLike, setIsLike] = useState(false);
const handleLike = ()=>{
if (!isLoggedIn){
alert("please login");
navigate("/login");
}
};
return (
<Link to=`/itemDetail/${ItemNo}`>
<img />
<button onClick={handleLike}>Like Image (heart)</button>
</Link>
)
};
The problem is I want to separate the case where user clicks like button and where user wants to see the detail of the post.
I just found a way to possibly solve this: placing input tag inside anchor tag seems to work! Seems like input tag ignores anchor tag that are nested around it. Do you think this is the solution?
This solution worked for me
function handleLike(e) {
// Add these two lines
e.stopPropagation();
e.preventDefault();
// Other logic of button click below
if (!isLoggedIn){
alert("please login");
navigate("/login");
}
}
return (
<Link to=`/itemDetail/${ItemNo}`>
<img />
<button onClick={handleLike}>Like Image (heart)</button>
</Link>
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With