【问题标题】:'dataURI' variable from inside React Hook useEffect will be lost after each renderReact Hook useEffect 内部的“dataURI”变量将在每次渲染后丢失
【发布时间】:2020-03-25 16:33:30
【问题描述】:

我想将下面用 vanilla javascript 编写的函数翻译成一个反应函数。下面的功能允许用户单击图像输入并将图像附加到包含类“.editor”的文本区域

 function getImage() {
      var file = document.querySelector("input[type=file]").files[0];

      var reader = new FileReader();

      let dataURI;

      reader.addEventListener(
        "load",
        function() {
          dataURI = reader.result;

          const img = document.createElement("img");
          img.src = dataURI;
          document.querySelector(".editor").appendChild(img);
        },
        false
      );

      if (file) {
        console.log("s");
        reader.readAsDataURL(file);
      }
    }

这是我迄今为止在我的反应组件中所做的......我收到以下错误消息

TypeError: editor.push 不是函数

Tools.js 组件:

function Toolbar() {
    const dispatch = useDispatch();

    let inputRef = useRef(null);
    const editor = useRef(null);
    const [selectedFile, setSelectedFile] = useState(null);

    const imgChangeHandler = e => {

        e.preventDefault();
        setSelectedFile(e.target.files[0]);

        let reader = new FileReader();
        let dataURI = reader.result;

        const img = React.createElement("img",{src: dataURI});
        editor.push(img);

        if(selectedFile) {
            console.log("s");
            reader.readAsDataURL(selectedFile)
        }
    };

Editor.js 组件:

 <>
            <div className="center">
               <div className="editor" ref={editor} style={editor} contentEditable={true} suppressContentEditableWarning={true}>
                   <h1>{introText}</h1>
                   <p>{subText}</p>
               </div>
            </div>
        </>

【问题讨论】:

  • 你能展示你的整个渲染函数吗?,编辑器变量也指的是什么
  • editor 在我的编辑器组件中使用 ref 编辑器引用 div。我已经包含了完整的功能和被引用的 div。

标签: reactjs use-effect mutable-reference


【解决方案1】:

不要

editor.push(img);

editor.current.push(img);

关于反应文档说

本质上,useRef 就像一个“盒子”,可以在其 .current 属性中保存一个可变值。

你需要在 ref 上使用 .current 属性

在包含 Editor.js 和 Toolbar.js 组件的父组件中,ref 变量应该在那里,就像这样

import Toolbar from "./toolbar.js"
import Editor from "./editor.js"

const parent = () => {
  const editorRef = useRef(null);

  /// other code for your component 

  return (
      <div>
          <Editor
            // other props
            editorRef = {editorRef}
            />
          <Toolbar  
            // other props
            editor= {editorRef}
          />
      </div>
   );


}

并在 Editor.js 文件中执行此操作

Editor.js


const editorComponent = props => {

    // some other code

   return (
       <div ref={props.editorRef}> // will be available because we are passing it 
                                   // in the parent component
       </div>

    );
}

现在在你的

Toolbar.js你可以通过这种方式访问​​ editor 属性。

props.editor

这将起作用,因为editor 存储在包含ToolbarEditor 组件的组件中。

【讨论】:

  • 当我将 ref={editor} 添加到我的 div 时。我收到一个错误“编辑器”未定义。这是因为我的函数在我的 Toolbar.js 组件中,而引用 ref={editor} 的 div 在我的 Editor.js 组件中。如何更新我的代码,使编辑器在两个组件中都是全局且可访问的?
  • 好吧,你需要将编辑器的 ref 存储在包含编辑器和收费栏的组件中
  • 我更新了代码。我现在收到 props.editorRef.current.push 不是函数
猜你喜欢
  • 2021-07-06
  • 2020-07-02
  • 2019-12-01
  • 2022-11-26
  • 2023-03-18
  • 2020-08-16
  • 1970-01-01
  • 2020-05-25
  • 2020-11-22
相关资源
最近更新 更多