iPhone 8 randomly locks the screen while I'm using it
Solution 1:
How often does this occur throughout the day or week?
Try to backup your iPhone and then restore it with iTunes. Once you get a chance to setup the iPhone then restore from the backup and then test the phone again. Usually this is a software glitch.
If it occurs afterwards do the restore again and don't restore your backup this time. Use the phone without the backup to test the issue. If its still present then it is a hardware sensor issue.