为什么我不能在JavaScript中创建自己的自定义钩子?

huangapple go评论54阅读模式
英文:

Why I cannot create my own custom hook in js?

问题

这是您的代码的中文翻译:

我试图将我的编辑器制作成一个自定义钩子,所以我创建了名为'useEditor'的钩子,如下所示:

import { useState, useEffect } from 'react';
import ReactQuill from 'react-quill';
import MathEditor from './MathEditor';

// 其他一些导入

function useEditor() {
  const [editorHtml, setEditorHtml] = useState('');
  const [placeholder, setPlaceholder] = useState('写点什么...');
  const [mathEditorVisible, setMathEditorVisible] = useState(false);
  const [expressionEditable, setExpressionEditable] = useState(false);

  const handleChange = (html) => {
    setEditorHtml(html);
  };

  // 其他一些函数

  const Editor = () => (
    <div>
      <ReactQuill
        onChange={handleChange}
        value={editorHtml}
        modules={modules}
        bounds='.app'
        placeholder={placeholder}
      />
      {mathEditorVisible && (
        <MathEditor
          onSaveFormula={handleSaveFormula}
          setMathEditorVisible={setMathEditorVisible}
          expressionEditable={expressionEditable}
          setExpressionEditable={setExpressionEditable}
          initialLatex=''
        />
      )}
    </div>
  );

  return {
    Editor,
    handleChange
  };
}

export default useEditor;

但在编译时出现以下错误:

*** 对象不被视为有效的React子元素(找到了具有键{Editor, handleChange}的对象)。如果您打算呈现一组子元素,请改用数组。 ***

英文:

I was trying to make my Editor a custom hook so I created 'useEditor' hook like this

import { useState, useEffect } from &#39;react&#39;;
import ReactQuill from &#39;react-quill&#39;;
import MathEditor from &#39;./MathEditor&#39;;

  //some other imports
    
function useEditor() {
  const [editorHtml, setEditorHtml] = useState(&#39;&#39;);
  const [placeholder, setPlaceholder] = useState(&#39;Write something...&#39;);
  const [mathEditorVisible, setMathEditorVisible] = useState(false);
  const [expressionEditable, setExpressionEditable] = useState(false);

  const handleChange = (html) =&gt; {
    setEditorHtml(html);
  };

  //some other functions 

  const Editor = () =&gt; (
    &lt;div&gt;
      &lt;ReactQuill
        onChange={handleChange}
        value={editorHtml}
        modules={modules}
        bounds=&#39;.app&#39;
        placeholder={placeholder}
      /&gt;
      {mathEditorVisible &amp;&amp; (
        &lt;MathEditor
          onSaveFormula={handleSaveFormula}
          setMathEditorVisible={setMathEditorVisible}
          expressionEditable={expressionEditable}
          setExpressionEditable={setExpressionEditable}
          initialLatex=&#39;&#39;
        /&gt;
      )}
    &lt;/div&gt;
  );

  return {
    Editor,
    handleChange
  };
}

export default useEditor;

But when compiling this error occurs

*** Objects are not valid as a React child (found: object with keys {Editor, handleChange}). If you meant to render a collection of children, use an array instead. ***

答案1

得分: 1

你没有导出Editor,你导出的是useEditor。你必须调用useEditor来获取Editor

import useEditor from './components/useEditor';
function App() {
  const { Editor } = useEditor();
  return (
    <div className='app'> <Editor /> </div>
  );
} 
英文:

You are not exporting Editor, you are exporting useEditor. You have to call useEditor to get Editor

import useEditor from &#39;./components/useEditor&#39;;
function App() {
  const { Editor } = useEditor();
  return (
    &lt;div className=&#39;app&#39;&gt; &lt;Editor /&gt; &lt;/div&gt;
  );
} 

huangapple
  • 本文由 发表于 2023年7月6日 20:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定