Getting "Object is of type 'unknown'." error on location.state

Viewed 1091

I'm using the useLocation hook like this -> const location = useLocation();. Then I try to pass it to the prop of a React element like this:

 <DatePicker
     emphasize={location.state?.fields?.some(
         (f: string) => f === 'endDate'
     )}
     data-testid="collection-form-end-date"
     type="datetime-local"
     {...field}
 />

But I get the location.state underscored and the following error -> Object is of type 'unknown'.

1 Answers

In my opinion to handle this issue you can try these two ways: On the first try, refer to the following question link and test its answer: React TypeScript: Correct type for useLocation() from react-router-dom . If it didn't work for you properly, try following way using lodash library:

import get from 'lodash/get'
 
 ...
 
 <DatePicker
 get(location, 'state.procedureDetail.description', '')
     emphasize={get(location, 'state.some') &&
       get(location, 'state.some')(
           (f: string) => f === 'endDate'
       )
     }
     data-testid="collection-form-end-date"
     type="datetime-local"
     {...field}
 />

Related