Fix React Error on setState while rendering a different component

This commit is contained in:
Eric NGUYEN 2022-10-13 18:14:54 +02:00
parent b3a3be0ba2
commit f5ec81d22b
2 changed files with 26 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import { Dispatch, SetStateAction } from 'react';
import { Dispatch, SetStateAction, useEffect } from 'react';
import { IConfiguration } from '../../../Interfaces/IConfiguration';
import { FetchConfiguration } from '../../API/api';
import { IEditorState } from '../../../Interfaces/IEditorState';
@ -7,18 +7,10 @@ import { DISABLE_API, GetDefaultEditorState } from '../../../utils/default';
export function NewEditor(
editorState: IEditorState,
setEditorState: Dispatch<SetStateAction<IEditorState>>,
setLoaded: Dispatch<SetStateAction<boolean>>
setEditorState: (newState: IEditorState) => void,
enableLoaded: () => void
): void {
if (DISABLE_API) {
setLoaded(true);
return;
}
if (editorState.configuration !== undefined) {
setLoaded(true);
return;
}
UseLoadOnBoot(enableLoaded, editorState);
// Fetch the configuration from the API
FetchConfiguration()
@ -27,13 +19,25 @@ export function NewEditor(
const editorState: IEditorState = GetDefaultEditorState(configuration);
setEditorState(editorState);
setLoaded(true);
enableLoaded();
}, (error) => {
console.debug('[NewEditor] Could not fetch resource from API. Using default.', error);
setLoaded(true);
enableLoaded();
});
}
function UseLoadOnBoot(enableLoaded: () => void, editorState: IEditorState): void {
useEffect(() => {
if (DISABLE_API) {
enableLoaded();
return;
}
if (editorState.configuration !== undefined) {
enableLoaded();
}
}, []);
}
export function LoadEditor(
files: FileList | null,
setEditorState: Dispatch<SetStateAction<IEditorState>>,