Cat keeps hitting me

Ok so I’m fostering a cat. I’ve had her for 8 days and she’ll come out for some love and pets and then all of a sudden will just hit me and then run and hide. But then she’ll come right back out and be all happy and then hit me and hide. It just happens over and over and I’m really confused. She wants love and gets super happy when I pet her but then she’ll hit me and I’m just not sure why. I haven’t noticed any patterns so it anyone has any idea please let me know!!