如何使用tyspript使用React-Input-2

发布于 2025-01-29 05:29:22 字数 1397 浏览 1 评论 0原文

我是打字稿的新手,但我正在强迫自己编程,因为他们说这是“最好的”练习,从长远来看...

所以现在我试图使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨笨の傻瓜 2025-02-05 05:29:22

问题是您不给usestate提供类型或默认值。这意味着默认值是未定义的,这是该状态唯一允许的类型。

这意味着类型setValue这里是(或多或少):

(newValue: undefined) => void

onChange prop的类型是:

(
  value: string,
  data: {} | CountryData,
  event: React.ChangeEvent<HTMLInputElement>, formattedValue: string
) => void

因此,第一个参数onChange将是字符串,但是您的setValue函数仅接受undefined


要修复它,您只需要给您的州一种类型即可。 Either explicitly:

const [value, setValue] = useState<string | undefined>()

Or by letting the type be inferred from the default value:

const [value, setValue] = useState('') // type inferred from default value

请参阅操场

The problem is that you don't give a type or default value to useState. This means the default value is undefined and that is the only type that will be allowed for that state.

That means that the type setValue here is (more or less):

(newValue: undefined) => void

And the type of the onChange prop is:

(
  value: string,
  data: {} | CountryData,
  event: React.ChangeEvent<HTMLInputElement>, formattedValue: string
) => void

So the first argument to onChange will be string, but your setValue function only accepts undefined.


To fix it, you just have to give your state a type. Either explicitly:

const [value, setValue] = useState<string | undefined>()

Or by letting the type be inferred from the default value:

const [value, setValue] = useState('') // type inferred from default value

See playground

无戏配角 2025-02-05 05:29:22

错误指出您的类型不兼容。通常,来自usestate()返回的第二个参数是类型dispatch&setStateAction&gt;onChange参数需要其他内容。这种最小的变化应该有效:

<PhoneInput
  placeholder='Enter phone number'
  value={value}
  onChange={(newValue) => setValue(newValue)}
  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'
/>

The error states that you have incompatible types. Usually, the 2nd argument from a useState() return is of type Dispatch<SetStateAction>. The onChange argument requires something else. This minimal change should work:

<PhoneInput
  placeholder='Enter phone number'
  value={value}
  onChange={(newValue) => setValue(newValue)}
  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'
/>
居里长安 2025-02-05 05:29:22
const [phoneNumber, setPhoneNumber] = useState<string>('');

const handlerPhoneNumber = (
    value: string,
    data: CountryData,
    event: ChangeEvent<HTMLInputElement>,
    formattedValue: string
    ) => {
        setPhoneNumber(value);
    };


<PhoneInput
    enableAreaCodeStretch
    country="br"                            
    inputProps={{
      name: 'phoneNumber',
    }}
    autoFormat
    regions={['south-america']}
    onlyCountries={['br', 'cl', 'uy', 'co']}
    areaCodes={{
      cl: ['56'],
      uy: ['598'],
      co: ['57'],
      br: ['55'],
    }}
    masks={{
      cl: '.. . ....-....',
      uy: '.. ....-....',
      co: '. ....-....',
      br: '(..) .....-....',
    }}
    specialLabel=""
    value={phoneNumber}
    onChange={handlerPhoneNumber}
    placeholder="Número de telefone"
/>
const [phoneNumber, setPhoneNumber] = useState<string>('');

const handlerPhoneNumber = (
    value: string,
    data: CountryData,
    event: ChangeEvent<HTMLInputElement>,
    formattedValue: string
    ) => {
        setPhoneNumber(value);
    };


<PhoneInput
    enableAreaCodeStretch
    country="br"                            
    inputProps={{
      name: 'phoneNumber',
    }}
    autoFormat
    regions={['south-america']}
    onlyCountries={['br', 'cl', 'uy', 'co']}
    areaCodes={{
      cl: ['56'],
      uy: ['598'],
      co: ['57'],
      br: ['55'],
    }}
    masks={{
      cl: '.. . ....-....',
      uy: '.. ....-....',
      co: '. ....-....',
      br: '(..) .....-....',
    }}
    specialLabel=""
    value={phoneNumber}
    onChange={handlerPhoneNumber}
    placeholder="Número de telefone"
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文