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'.
Managed to figure this out by adding the field to useLocation()
const location = useLocation<{
fields: unknown;
}>();
I don't know if it will work or not but you can try.
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}
/>