Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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'.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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}
 />

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!