代码之家  ›  专栏  ›  技术社区  ›  darro911

简单反应useRef示例错误

  •  0
  • darro911  · 技术社区  · 3 年前

    有这么简单的 React 应用程序:

    import React, { useState, useEffect, useRef } from "react";
    import ReactDOM from 'react-dom'
    
    function App() {
      const [inputValue, setInputValue] = useState("");
      const count = useRef(0);
    
      useEffect(() => {
        count.current = count.current + 1;
      });
    
      return (
        <>
          <input
            type="text"
            //value={inputValue}
            //onChange={(e) => setInputValue(e.target.value)}
          />
          <h1>Render Count: {count.current}</h1>
        </>
      );
    }
    

    const rootElement=document。getElementById('root') 反应状态。渲染(,根元素)

    我发现了一些错误:

    (0,_react.useState)不是函数(0,_react.useRef)不是 作用

    我做错了什么?

    1 回复  |  直到 3 年前
        1
  •  1
  •   mc-user    3 年前

    React挂钩在16.8版本中引入。所以你不能用 useState , useEffect 以及<react上的16.8版本。

    Documentation