Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Editor isn't ready yet and setting content not working #175

Open
mbpictures opened this issue Jul 29, 2024 · 15 comments
Open

[BUG]: Editor isn't ready yet and setting content not working #175

mbpictures opened this issue Jul 29, 2024 · 15 comments

Comments

@mbpictures
Copy link

Describe the bug
I'm using the useEditorBridge() hook inside a modal component and on the mount of the component, I'm getting an "Editor isn't ready yet" warning.

To Reproduce
This is my modal component:

export const TextEditorModal = ({
    isVisible,
    onClose,
    content
}: {
    isVisible: boolean,
    onClose: (text: string) => unknown,
    content?: string
}) => {
    const theme = useTheme();

    const editor = useEditorBridge({
        initialContent: content
    });

    useEffect(() => {
        if (!content || !isVisible) {
            return;
        }
        editor.setContent(content);
    }, [content, isVisible]);

    const handleClose = () => {
        editor.getHTML().then(onClose);
    }

    return (
        <Modal
            style={{
                backgroundColor: theme.colors.background,
                padding: 12,
                margin: 0,
                width,
                height
            }}
            isVisible={isVisible}
            onBackButtonPress={handleClose}
        >
            <CustomIconButton icon={"chevron-left"} onPress={handleClose} />
            <RichText editor={editor} />
            <KeyboardAvoidingView
                behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
                style={exampleStyles.keyboardAvoidingView}
            >
                <Toolbar editor={editor} />
            </KeyboardAvoidingView>
        </Modal>
    )
};

Expected behavior
Content is updating once the useEffect hook triggers the setContent method. No warnings.

Screenshots

@17Amir17
Copy link
Collaborator

17Amir17 commented Jul 30, 2024

Hey @mbpictures the issue here is that it takes some time for the editor to initialize and accept messages, that warning is thrown because a message was sent before the editor can do anything about it.

You can know the editor is ready from the editor state, try update your code to

    useEffect(() => {
        if (!content || !isVisible || !editor.getEditorState().isReady) {
            return;
        }
        editor.setContent(content);
    }, [content, isVisible]);

Of course this will not call setContent if it is not ready yet, but the initial content should be loaded from the prop anyways, alternatively you could list for isReady updates with useBridgeState

@mbpictures
Copy link
Author

Thanks for this hint!

Even when I update my code like this, I'm getting the same warning:

    const editor = useEditorBridge({
        initialContent: content
    });
    const editorState = useBridgeState(editor);

    useEffect(() => {
        if (!content || !isVisible || !editorState.isReady) {
            return;
        }
        editor.setContent(content);
    }, [isVisible, editorState.isReady]);

@jonhy751
Copy link

jonhy751 commented Sep 3, 2024

I also have this error

@mbpictures
Copy link
Author

Update: even without setting the content at all I get the warning

@17Amir17 17Amir17 reopened this Sep 3, 2024
@17Amir17
Copy link
Collaborator

17Amir17 commented Sep 3, 2024

I'm unable to reproduce this, could you share a clean repo where this happens so I can investigate?

@jonhy751
Copy link

jonhy751 commented Sep 3, 2024

I'm creating

@17Amir17
Copy link
Collaborator

17Amir17 commented Sep 4, 2024

If this is only happening on new versions of expo, this might be caused from the isExpo util` not working
https://github.com/10play/10tap-editor/blob/main/src/utils/misc.ts

EDIT: sorry this is not related, the best way to go forward would be to provide me with a repo that reproduces it

@mbpictures
Copy link
Author

I'm using a bare react native project, with one expo-module integrated (expo-av)

@jonhy751
Copy link

jonhy751 commented Sep 4, 2024

I realized that the editor needs an input to be rendered together.
In my code, the editor was rendered first and after a few milliseconds my input was rendered on the screen, causing the error.
I managed to solve it by setting the input to render right when the component started, together with the editor.

@ryzencool
Copy link

I realized that the editor needs an input to be rendered together. In my code, the editor was rendered first and after a few milliseconds my input was rendered on the screen, causing the error. I managed to solve it by setting the input to render right when the component started, together with the editor.

I have encountered the same issue. Could you please share your code? Thank you very much.

@humungasaurus
Copy link

I'm also encountering this, would love some code examples of how it is solved

@hesamzkr
Copy link

Same I'm also having this issue and I'm not sure what's causing it or how to solve it

@17Amir17
Copy link
Collaborator

Hey guys if you could provide a repo with the min amount of code to reproduce this we will have a better chance at solving this, since we cannot reproduce

@humungasaurus
Copy link

Too difficult to create a minimal repo, but I was able to repro this by removing then re-adding the conditional rendering of the RichText view. Basically, it seems that if you instantiate the editor but dont render the webview, you get this warning. However, if you instantiate the editor after the conditional you run into the "Renderred fewer hooks than expected" error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants