I am facing an issue when I run a React Freshdesk app locally (or at least locally) with the latest fdk 6.11.0.
For some reason the app.initialized() promise never gets resolved, there are no errors in .catch() , and even nothing in .finally() .
The global variable app is accessible and the initialized() method is available when I log it. setTimeout() didn’t help.
What is the difference?
The app variable is global (in window) anyway.
I’ve tried to use the app.initialized() without window previously while trying to resolve the issue. So changing it back won’t fix the issue, unfortunately.
Also, my current project is written in TypeScript, so it is important for me to keep this window for that purposes, but this is not the reason anyway
I’ve tried refreshing the page differently, cleaning cache - nothing helps.
I’ve already faced this issue already some time ago (sample app, no TypeScript) and have even created an issue on GitHub.
The only solution for now is simply including the <script> into the HTML template. However, I would still prefer to include it the React.useEffect() way if possible.
Can you make the callback method inside useEffect async method and use awaits inside
This is just to verify if there are problems with chaining promises inside useEffect
Also, usually, only state variables are given as an array in the second argument in useEffect. Why are you sending setFdClient which seems to be the event handler for the state variable?
The useEffect() should be fine for Promises and chaining. Just to be clear - the reason why I’ve created this thread is because I want to try setting up the external script the same way as provided in the sample React Todo App. I’ve explored different ways of including external scripts in React and this way seems to be quite a popular one, however it does not work in my case specified above (multiple different apps tested, including the mentioned sample demo app).
Everything works perfectly when the script is simply inserted as an external one in the template.html file.
Other Promise chains work fine in different parts of the app. The piece of code provided above is related to a React Context file (FreshdeskClientContext).
Here is a part of the <App/> component of the very same project for example. It uses Promise chaining in useEffect(), too:
As for the second note regarding the setState() (or setFdClient() in my case) - you are right, that’s my bad. In the example above both variables fdClient and setIparams from destructuring the object from useContext() are required by the ESlint rule to be included into the useEffect() dependencies array.
I got confused by this and accidentally included the setState()-like functions as useEffect() dependencies in the previous code example (FreshdeskClientContextProvider).
I’ve removed them from these arrays. Thank you for noticing this!
I am still not sure if I understand the purpose yet. are you trying to pass the client as a context instead of passing it as props? is that your requirement? or is there something else you are trying here?
and what is the instance FresdeskClient(client) could you log something before const fd = new FresdeskClient(client) inside the then() and check if there are any logs?
if using the context API is the requirement I am also not sure why is it?
useEffect(() => {
if (!loaded) return;
window.app.initialized()
.then((client) => {
const fd = new FresdeskClient(client);
setFdClient(fd);
})
.catch((error) => {
console.error('[ERROR] Could not init app', error)); // <-- Never gets logged
});
}, [loaded, setFdClient]);
and not just set the client directly to the state?
useEffect(() => {
if (!loaded) return;
window.app.initialized()
.then((client) => {
setFdClient(client);
})
.catch((error) => {
console.error('[ERROR] Could not init app', error)); // <-- Never gets logged
});
}, [loaded, setFdClient]);
The purpose of my whole thread is to include the external script using the suggested React useEffect approach and to understand why the app.initialized() does not return anything.
Yes, I am storing the Freshdesk client in Context, because I don’t want to get the “props drilling” which is sort of anti-pattern - I want to be able to use the Freshdesks’s client-based class where necessary only and not to pass it with props (obviously).
This FreshdeskClient class contains all required wrappers for the client methods. For example, wrappers for making requests, so that they are sent in a more convenient comfortable way, e.g. fdClient.getTicketFields(). I can provide the code of the FreshdeskClient if required - however, there is nothing special there - just a constructor and a setter.
To be honest, I am a little confused how can this be relevant to the raised issue. All of this works when the Freshdesk client JS script is included into the HTML template and does not work with the provided sample approach due to the never-resolving app.initialized().
Hi
Reviving this thread after some time. useEffect seems to be a bit abrupt with promises. Using useLayoutEffect resolves the issue based on local testing. Can others try it out and confirm so that we can change the code in the template that is supplied with FDK?