use-dynamic-refs icon indicating copy to clipboard operation
use-dynamic-refs copied to clipboard

Example throws errors in create-react-app typescript template

Open harrisonturton opened this issue 4 years ago • 5 comments

Hello!

This looks like an incredibly useful little package, but I can't seem to get it working.

I ran npx create-react-app use-multiple-refs --template typescript projects and tried the example. Specifically this:

import React, { useEffect } from "react";
import useDynamicRefs from "use-dynamic-refs";

const App: React.FC = () => {

	const [ getRef, setRef ] = useDynamicRefs();

	return (
		<>
			<input type="text" placeholder="Write..." ref={setRef("input")}/>
		</>
	);
};

export default App;

And I get these errors:

/Users/.../Documents/dev/multi-refs/src/App.tsx
TypeScript error in /Users/.../Documents/dev/multi-refs/src/App.tsx(10,46):
Type 'void | RefObject<unknown>' is not assignable to type 'string | ((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null | undefined'.
  Type 'void' is not assignable to type 'string | ((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null | undefined'.  TS2322

      8 | 	return (
      9 | 		<>
 > 10 | 			<input type="text" placeholder="Write..." ref={setRef("input")}/>
         | 			                                                                    ^
     11 | 		</>
     12 | 	);
     13 | };

Are there type annotations I'm missing? I had a look in your source, but couldn't quite figure it out.

harrisonturton avatar Apr 25 '20 02:04 harrisonturton

Hey there, I noticed this too this morning, I'll take a look and push an update ASAP

fitzmode avatar Apr 25 '20 12:04 fitzmode

Hi @fitzmode! is there any news about this?

sclavijo93 avatar Sep 08 '20 19:09 sclavijo93

I'm working with React Native 0.63 and use-dynamic-refs 1.0.0

sclavijo93 avatar Sep 08 '20 19:09 sclavijo93

This should work, ref={setRef("input") as RefObject<HTMLInputElement>}

marklude avatar Mar 11 '21 09:03 marklude

ref={setRef("input") as RefObject<HTMLInputElement>} this works for me

arturmoroz avatar Jun 25 '22 11:06 arturmoroz