how to control an object's property in animation
Just add a driver to your viewport visibility like this:

Then copy the driver and paste it to every object you need. After each copy just adapt the new pasted driver in this way: change the object (in my case cube) to that object you just added the driver to.
… but better is @batFINGER solution here: How can you make it automatically invisible to render if its already invisible in viewport?