With React, I want to access to a URL parameter inside an Async function.
I have been using React route as following :
return (
<BrowserRouter>
<Routes>
<Route path="/signin/:projectUUID" element={<SignIn />} />
I have been trying to access the projectUUID parameter from an asycn function as follows:
export const EmailSendEmailMagicLink = async (callback) =>
{
let resultState = { state: '', data: {} };
const { projectUUID } = this.props; // ( 45th line is here )
let project_uuid = projectUUID ? projectUUID : "";
(Update the question as requested below) This is where I invoke the function :
const EmailSignInBtn = async (form_data, file) =>
{
let result = await EmailSendEmailMagicLink();
And this is HTML :
<Box className='signup-Btn' onClick={EmailSignInBtn}>
Get Magic Link
</Box>
However, when I execute the request, I get the following error :
ApiProvider.js:45 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'props')
at EmailSendEmailMagicLink (ApiProvider.js:45:1)
at EmailSignInBtn (signIn.js:42:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
at invokeGuardedCallback (react-dom.development.js:4277:1)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4291:1)
at executeDispatch (react-dom.development.js:9041:1)
at processDispatchQueueItemsInOrder (react-dom.development.js:9073:1)
at processDispatchQueue (react-dom.development.js:9086:1)
at dispatchEventsForPlugins (react-dom.development.js:9097:1)
What shall I do to access the url argument from / within the async function ?
Note :
When I use useParams as following
const { projectUUID } = useParams();
let project_uuid = projectUUID ? projectUUID : "";
I get the following error :
Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
propsinEmailSignInBtnfunction, maybe you should try thisawait EmailSendEmailMagicLink.call(this).SignInis what needs to access the route params.