React stateless component this.refs..value?

后端 未结 7 1697
故里飘歌
故里飘歌 2020-12-03 01:09

I don\'t know if I\'m doing this correctly... If I want to get value from an input I use this.refs.whatever.value.trim() but if that input is a stateless function component

7条回答
  •  失恋的感觉
    2020-12-03 01:18

    as of react 16.8 you can use the useRef hook, from the docs:

    useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.

    function TextInputWithFocusButton() {
      const inputEl = useRef(null);
      const onButtonClick = () => {
        // `current` points to the mounted text input element
        inputEl.current.focus();
      };
      return (
        <>
          
          
        
      );
    }
    

提交回复
热议问题