React-typescript中ref定义

示例:

  

 1 const Home: React.FC = () => {
 2     const textRef = useRef<HTMLInputElement & HTMLTextAreaElement>(null);
 3 
 4     useEffect(() => {
 5         console.log(textRef.current && textRef.current.value.indexOf('\n'));
 6     }, []);
 7 
 8     return (
 9         <div className='home'>
10             <header className='home-header'>
11                 <Header />
12             </header>
13             <main className='home-main'>
14                 <aside></aside>
15                 <article>
16                     <textarea ref={textRef as React.RefObject<HTMLTextAreaElement>} defaultValue='撒大苏打实打实广发而高盛公司的发生法发案说法沙俄噶尔'></textarea>
17                 </article>
18             </main>
19         </div>
20     );
21 };
22 
23 export default Home;

 

上一篇:TypeScript学习


下一篇:TypeScript 是弱类型