簡體   English   中英

react-rte 給出 TypeError: r.getEditorState is not a function in next js

[英]react-rte giving TypeError: r.getEditorState is not a function in next js

我有一個 nextjs 項目,它有一個 react-rte 組件,react-rte 組件顯示正確,但是當我轉到其他組件並單擊瀏覽器后退按鈕時,我收到以下錯誤:

Unhandled Runtime Error TypeError: r.getEditorState is not a function

當我注釋掉 react-rte 組件時,錯誤不再發生

反應組件

import React, { useState, useEffect } from "react";
import dynamic from "next/dynamic";
import PropTypes from "prop-types";
//import the component
const RichTextEditor = dynamic(() => import("react-rte"), { ssr: false });

const MyStatefulEditor = ({ onChange }) => {
  const [value, setValue] = useState([]);
  console.log(value.toString("html"));

  useEffect(() => {
    const importModule = async () => {
      //import module on the client-side to get `createEmptyValue` instead of a component
      const module = await import("react-rte");
      console.log(module);
      setValue(module.createEmptyValue());
    };
    importModule();
  }, []);

  const handleOnChange = (value) => {
    setValue(value);
    if (onChange) {
      onChange(value.toString("html"));
    }
  };

  return <RichTextEditor value={value} onChange={handleOnChange} />;
};

MyStatefulEditor.propTypes = {
  onChange: PropTypes.func,
};

export default MyStatefulEditor;

嘗試這個

import React, { useState, useEffect } from "react";
import dynamic from "next/dynamic";
import PropTypes from "prop-types";

const RichTextEditor = dynamic(() => import("react-rte"), { ssr: false });


const MyStatefulEditor = ({ onChange }) => {
  const [value, setValue] = useState([]);
  
  useEffect(() => {
    // set the state value using the package available method
    setValue(RichTextEditor.createEmptyValue())
  }, []);

  const handleOnChange = (value) => {
    setValue(value);
    if (onChange) {
      onChange(value.toString("html"));
    }
  };

  return <RichTextEditor value={value} onChange={handleOnChange} />;
};

MyStatefulEditor.propTypes = {
  onChange: PropTypes.func,
};

export default MyStatefulEditor;

就像我在之前的評論中提到的那樣,我注意到您正在導入react-rte包兩次。

useEffect鈎子中,您可以通過查看此處找到的示例代碼進行導入以初始化value狀態

您可以使用來自已導入包的RichTextEditor.createEmptyValue()來實現。

您會注意到我更改了導入,不是動態的,嘗試一下,如果它有效,那么如果這是您需要的,請嘗試進行動態導入。

您可以在渲染RichTextEditor之前添加條件來檢查value

import React, { useState, useEffect } from "react";
import dynamic from "next/dynamic";
import PropTypes from "prop-types";
import { useRouter } from "next/router";
//import the component
const RichTextEditor = dynamic(() => import("react-rte"), { ssr: false });

const MyStatefulEditor = ({ onChange }) => {
  const [value, setValue] = useState();
  const router = useRouter();
  useEffect(() => {
    const importModule = async () => {
      //import module on the client-side to get `createEmptyValue` instead of a component
      const module = await import("react-rte");
      setValue(module.createEmptyValue());
    };
    importModule();
  }, [router.pathname]);

  const handleOnChange = (value) => {
    setValue(value);
    if (onChange) {
      onChange(value.toString("html"));
    }
  };

  //if `value` from react-rte is not generated yet, you should not render `RichTextEditor`
  if (!value) {
    return null;
  }

  return <RichTextEditor value={value} onChange={handleOnChange} />;
};

MyStatefulEditor.propTypes = {
  onChange: PropTypes.func
};

export default MyStatefulEditor;

您可以使用實時示例沙箱驗證實現

Office js 類型錯誤:#<object> 不是反應組件中的 function<div id="text_translate"><p> 我創建了一個反應組件並構建了它。 在構建導入到 HTML 頁面並在 Word 加載項的任務窗格中呈現頁面之后。 我添加了多個按鈕以將段落插入到 word 文檔中,如果單擊按鈕,我會看到以下錯誤:</p><p> js_includes_doctype.jsx?v=06-30-2022_1211&amp;lp=Mon_Aug_29_10_38_28_PDT_2022&amp;c=8_108:3 Uncaught (in promise) TypeError: #&lt;Object&gt; is not a function at Array.forEach (&lt;anonymous&gt;) at Array.c [as each] (js_includes_doctype.jsx?v=06-30-2022_1211&amp;lp=Mon_Aug_29_10_38_28_PDT_2022&amp;c=8_108:3:14458) at OSF.DDA.ApiMethodCall.constructCallArgs (word-web-16.00.js:25:65928) at OSF.DDA.AsyncMethodCall.verifyAndExtractCall (word-web-16.00.js:25:67509) at &lt;computed&gt; (word-web-16.00.js:25:80872) at Object.executeRichApiRequestAsync (word-web-16.00.js:25:89680) at word-web-16.00.js:25:410703 at new Promise (&lt;anonymous&gt;) at t.executeAsync (word-web-16.00.js:25:410658) at o.syncPrivate (word-web-16.00.js:25:386161)</p><p> 這是我在組件中添加的反應代碼:</p><pre> import '@servicenow/now-button'; const callApi = () =&gt; { Word.run(async (context) =&gt; { const paragraph = context.document.body.insertParagraph("This is para", "End"); let contentControl = paragraph.insertContentControl(); paragraph.font.color = "blue"; await context.sync(); }); } export const initializeOffice = () =&gt; new Promise((resolve, reject) =&gt; { console.log("ASdadqdadsa") try{ Office.initialize = resolve; }catch(e){ reject(); } }); const view = (state, { updateState, dispatch }) =&gt; { return ( &lt;div&gt; {initializeOffice()} &lt;button label="Insert3" variant="primary" size="md" icon="" config-aria={{}} tooltip-content="" on-click={callApi}&gt;&lt;/button&gt; &lt;/div&gt; ) }; export default view;</pre><p> 我沒有權限直接在加載項任務窗格上呈現組件,因此我必須將其導入 HTML 頁面並呈現組件。 我正在將 office js 腳本作為script標簽加載到 HTML 頁面上。 谷歌搜索和研究后找不到解決方案。 任何幫助都會很棒。 謝謝</p></div></object>

[英]Office js TypeError: #<Object> is not a function in react component

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Redux-Form和React-RTE-保存RTE editorValue到表單狀態? TypeError: Object(...) 不是一個函數 React js React教程給出TypeError React.js TypeError: Object(...) 不是函數 TypeError: .some 不是函數 React JS 錯誤 類型錯誤:……地圖不是 function - react.js TypeError: Object(…) is not a function React.js 類型錯誤:map 不是 function(使用 React.js) 反應:JS TypeError:items.map 不是 function Office js 類型錯誤:#<object> 不是反應組件中的 function<div id="text_translate"><p> 我創建了一個反應組件並構建了它。 在構建導入到 HTML 頁面並在 Word 加載項的任務窗格中呈現頁面之后。 我添加了多個按鈕以將段落插入到 word 文檔中,如果單擊按鈕,我會看到以下錯誤:</p><p> js_includes_doctype.jsx?v=06-30-2022_1211&amp;lp=Mon_Aug_29_10_38_28_PDT_2022&amp;c=8_108:3 Uncaught (in promise) TypeError: #&lt;Object&gt; is not a function at Array.forEach (&lt;anonymous&gt;) at Array.c [as each] (js_includes_doctype.jsx?v=06-30-2022_1211&amp;lp=Mon_Aug_29_10_38_28_PDT_2022&amp;c=8_108:3:14458) at OSF.DDA.ApiMethodCall.constructCallArgs (word-web-16.00.js:25:65928) at OSF.DDA.AsyncMethodCall.verifyAndExtractCall (word-web-16.00.js:25:67509) at &lt;computed&gt; (word-web-16.00.js:25:80872) at Object.executeRichApiRequestAsync (word-web-16.00.js:25:89680) at word-web-16.00.js:25:410703 at new Promise (&lt;anonymous&gt;) at t.executeAsync (word-web-16.00.js:25:410658) at o.syncPrivate (word-web-16.00.js:25:386161)</p><p> 這是我在組件中添加的反應代碼:</p><pre> import '@servicenow/now-button'; const callApi = () =&gt; { Word.run(async (context) =&gt; { const paragraph = context.document.body.insertParagraph("This is para", "End"); let contentControl = paragraph.insertContentControl(); paragraph.font.color = "blue"; await context.sync(); }); } export const initializeOffice = () =&gt; new Promise((resolve, reject) =&gt; { console.log("ASdadqdadsa") try{ Office.initialize = resolve; }catch(e){ reject(); } }); const view = (state, { updateState, dispatch }) =&gt; { return ( &lt;div&gt; {initializeOffice()} &lt;button label="Insert3" variant="primary" size="md" icon="" config-aria={{}} tooltip-content="" on-click={callApi}&gt;&lt;/button&gt; &lt;/div&gt; ) }; export default view;</pre><p> 我沒有權限直接在加載項任務窗格上呈現組件,因此我必須將其導入 HTML 頁面並呈現組件。 我正在將 office js 腳本作為script標簽加載到 HTML 頁面上。 谷歌搜索和研究后找不到解決方案。 任何幫助都會很棒。 謝謝</p></div></object>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM