Fires a custom event when the state changes.