UseEffect reads value correctly but value is undefined if accessed elsewhere