I have this component that renders the number of people that liked the book, however I cant get consistent result in my states.
Heres my code:
///this is for getting the book details
const {id} = useParams();
const dispatch = useDispatch();
const[hasliked, sethasliked] = useState(null);
useEffect(() => {
const bookData = {
id: id
}
dispatch(openBook(bookData))
}, [])
///and this is for setting the state of the component
const { bookFeatured, isLoading, isSuccess, openbook, likeStatus } = useSelector(state => state.book);
const { user, isError, message } = useSelector(state => state.auth);
useEffect(()=>{
if(openbook.noOfLikes?.find((noOfLikes) => noOfLikes === user._id)){
sethasliked(true)
}else{
sethasliked(false)
}
},[openbook])
const Likes = () => {
return hasliked
? (
<><FaHeart fontSize="small" /> {hasliked.toString()}</>
) : (
<><FaRegHeart fontSize="small" fill='white'/> {hasliked.toString()}</>
);
};
the thing here is i get inconsistent result when getting the value from const hasliked, if i already liked a book, it will sometimes tell me that the hasliked is false, but sometimes it will go to true, this caused me to get different result each time i render the component, I think it has something to do with the delay in useEffect due to async actions, Is there something that I can do about this?