如何使用tyspript使用React-Input-2
我是打字稿的新手,但我正在强迫自己编程,因为他们说这是“最好的”练习,从长远来看...
所以现在我试图使用React-Input-Input-2
https://wwwww.npmjs.com/package/package/react-phone-phone-phone-input-nput-2 < /a>
上面页面上的示例仅适用于标准JavaScript。 我确实遵循了它...小部件确实显示了...但是我无法在输入中输入任何内容... 在我的编辑器中,onChange
属性/Prop of Phone Intup标签的下面有一条红色的线条。 当我徘徊时,它显示以下错误:
(方法) CountryData,活动:React.ChangeEvent, formattedValue:string):void类型 'dispatch&lt; setStateaction&gt;'不能分配给类型 '(值:字符串,数据:{} | countrydata,事件: ChangeEvent,formattedValue:string)=&gt; void'。
参数的类型“值”和“值”是不兼容的。 类型“字符串”不可分配给'setTateAction'.ts(2322)index.d.ts(26,5):预期 类型来自属性“ on Change”,在此处声明 'intinsInsICATTRIBUTES&amp; PhoneInputProps'
我基本上将其放在顶部:
const [value, setValue] = useState()
然后在返回的JSX中的某个地方:
<PhoneInput
placeholder='Enter phone number'
value={value}
onChange={setValue}
className='block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md'
/>
如何调整代码,以便react-phone-Input-2适用于Typescript?
I am new to Typescript but I'm forcing myself to program in it because they say it is "best" practice and beneficial in the long run...
so now I'm trying to use react-phone-input-2
https://www.npmjs.com/package/react-phone-input-2
The example on the page above is for just standard Javascript.
I did follow it...the widget does show up...however I can't type anything inside the input...
In my editor the onChange
attribute/prop of the PhoneInput tag has a red squiggly line under it.
When I hover over it shows the following error:
(method) PhoneInputEventsProps.onChange?(value: string, data: {} |
CountryData, event: React.ChangeEvent,
formattedValue: string): void Type
'Dispatch<SetStateAction>' is not assignable to type
'(value: string, data: {} | CountryData, event:
ChangeEvent, formattedValue: string) => void'.
Types of parameters 'value' and 'value' are incompatible.
Type 'string' is not assignable to type 'SetStateAction'.ts(2322) index.d.ts(26, 5): The expected
type comes from property 'onChange' which is declared here on type
'IntrinsicAttributes & PhoneInputProps'
I basically have this at the top:
const [value, setValue] = useState()
Then somewhere in the returned JSX I have:
<PhoneInput
placeholder='Enter phone number'
value={value}
onChange={setValue}
className='block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md'
/>
How do I adjust the code so the react-phone-input-2 will work for Typescript????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您不给
usestate
提供类型或默认值。这意味着默认值是未定义的
,这是该状态唯一允许的类型。这意味着类型
setValue
这里是(或多或少):onChange
prop的类型是:因此,第一个参数
onChange
将是字符串
,但是您的setValue
函数仅接受undefined
。要修复它,您只需要给您的州一种类型即可。 Either explicitly:
Or by letting the type be inferred from the default value:
请参阅操场
The problem is that you don't give a type or default value to
useState
. This means the default value isundefined
and that is the only type that will be allowed for that state.That means that the type
setValue
here is (more or less):And the type of the
onChange
prop is:So the first argument to
onChange
will bestring
, but yoursetValue
function only acceptsundefined
.To fix it, you just have to give your state a type. Either explicitly:
Or by letting the type be inferred from the default value:
See playground
错误指出您的类型不兼容。通常,来自
usestate()
返回的第二个参数是类型dispatch&setStateAction&gt;
。onChange
参数需要其他内容。这种最小的变化应该有效:The error states that you have incompatible types. Usually, the 2nd argument from a
useState()
return is of typeDispatch<SetStateAction>
. TheonChange
argument requires something else. This minimal change should work: