zoukankan      html  css  js  c++  java
  • xml ui

    returns a 'ref' object.

    const refContainer = useRef(initialValueToBePersisted)
    

    Value is persisted in the refContainer.current property.

    values are accessed from the .current property of the returned object.

    The .current property could be initialised to an initial value e.g. useRef(initialValue)

    The object is persisted for the entire lifetime of the component.

    Essentially, useRef is like a “box” that can hold a mutable value in its .current property.

    You might be familiar with refs primarily as a way to access the DOM. If you pass a ref object to React with <div ref={myRef} />, React will set its .current property to the corresponding DOM node whenever that node changes.

    However, useRef() is useful for more than the ref attribute. It’s handy for keeping any mutable value around similar to how you’d use instance fields in classes.

    This works because useRef() creates a plain JavaScript object. The only difference between useRef() and creating a {current: ...} object yourself is that useRef will give you the same ref object on every render.

    Keep in mind that useRef doesn’t notify you when its content changes. Mutating the .current property doesn’t cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use a callback ref instead.

    function TextInputWithFocusButton() {
      const inputEl = useRef(null);
      const onButtonClick = () => {
        // `current` points to the mounted text input element
        inputEl.current.focus();
      };
      return (
        <>
          <input ref={inputEl} type="text" />
          <button onClick={onButtonClick}>Focus the input</button>
        </>
      );
    }
    
    () => {
      const textAreaEl = useRef(null);
      const handleBtnClick = () => {
        textAreaEl.current.value =
          "The is the story of your life. You are an human being, and you're on a website about React Hooks";
        textAreaEl.current.focus();
      };
      return (
        <section style={{ textAlign: "center" }}>
          <div>
            <button onClick={handleBtnClick}>Focus and Populate Text Field</button>
          </div>
          <label
            htmlFor="story"
            style={{
              display: "block",
              background: "olive",
              margin: "1em",
              padding: "1em"
            }}
          >
            The input box below will be focused and populated with some text
            (imperatively) upon clicking the button above.
          </label>
          <textarea ref={textAreaEl} id="story" rows="5" cols="33" />
        </section>
      );
    };
    
    () => {
        const textAreaEl = useRef(null);
        const stringVal = useRef("This is a string saved via the ref object --- ")
        const handleBtnClick = () => {
          textAreaEl.current.value =
          stringVal.current + "The is the story of your life. You are an human being, and you're on a website about React Hooks";
          textAreaEl.current.focus();
        };
        return (
          <section style={{ textAlign: "center" }}>
            <div>
              <button onClick={handleBtnClick}>Focus and Populate Text Field</button>
            </div>
            <label
              htmlFor="story"
              style={{
                display: "block",
                background: "olive",
                margin: "1em",
                padding: "1em"
              }}
            >
              Prepare to see text from the ref object here. Click button above.
            </label>
            <textarea ref={textAreaEl} id="story" rows="5" cols="33" />
          </section>
        );
      };
    
    function App() {
      const [query, setQuery] = React.useState("react hooks");
      // we can pass useRef a default value
      // we don't need it here, so we pass in null to ref an empty object
      const searchInput = useRef(null);
    
      function handleClearSearch() {
        // current references the text input once App mounts
        searchInput.current.value = "";
        // useRef can store basically any value in its .current property
        searchInput.current.focus();
      }
    
      return (
        <form>
          <input
            type="text"
            onChange={event => setQuery(event.target.value)}
            ref={searchInput}
          />
          <button type="submit">Search</button>
          <button type="button" onClick={handleClearSearch}>
            Clear
          </button>
        </form>
      );
    }
    
    

    https://react-hooks-cheatsheet.com/

  • 相关阅读:
    多元化时代敏捷软件开发的崛起与传统软件工程的延续
    敏捷软件开发与传统软件工程概述比较
    结构化方法和面向对象方法的比较
    sql server 的Maintenance Plans(维护计划)详解
    sql server 如何查询出数据库作业所有者的信息并完成批量替换
    sql server 运维时CPU,内存,操作系统等信息查询(用sql语句)
    sql server 数据导出(入)方法总结
    sql server 转置 和实现随机分配和一串代码的含义拼在一行
    python 之路初(一):pycharm 安装 和 环境配置 和 中文乱码问题
    Qt5.9.6 vs2015 SQlite 数据库增删改查
  • 原文地址:https://www.cnblogs.com/Searchor/p/13877944.html
Copyright © 2011-2022 走看看