I should have probably specified the context but the code example I shared is not coming from a perspective of using React.
I think with React the way to handle state is dictated by the library so there isn't much choice you have to make. You basically have to follow whatever pattern React gives you.