首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将自己定制的inputComponent JSX <input />添加到react-phone-number-input PhoneInput?

如何将自己定制的inputComponent JSX <input />添加到react-phone-number-input PhoneInput?
EN

Stack Overflow用户
提问于 2021-05-24 00:06:54
回答 1查看 237关注 0票数 1

代码遵循此描述。

react-phone-number-input允许用户将其默认的<input /> JSX标记替换为自己的标记,这特别需要类型React.ForwardRefExoticComponent<React.InputHTMLAttributes<HTMLInputElement> & React.RefAttributes<any>>forwardRef (以允许访问<input />)。

虽然我相信我已经在myTextInput中提供了这一点,但编译器抱怨说:

Property '$$typeof' is missing in type 'Element' but required in type 'ForwardRefExoticComponent<...Type 'Element' is not assignable to type 'ForwardRefExoticComponent<...

好的,myTextInput = <ForwardedInput确实返回了一个<input /> JSX,所以是的,它是一个React Element,因此错误消息是有意义的。但是上述标记将解析为React.ForwardRefExoticComponent<React.InputHTMLAttributes<HTMLInputElement> & React.RefAttributes<any>>,那么我如何让编译器识别它呢?

感谢您的阅读。

代码语言:javascript
复制
import React, { forwardRef, InputHTMLAttributes } from 'react';
import 'react-phone-number-input/style.css'
import PhoneInput from 'react-phone-number-input'

const ForwardedInput = forwardRef<any, InputHTMLAttributes<HTMLInputElement>>(
    (props, ref) => {
        const { onChange, value } = props;
        return (
            <input
                type="text"
                ref={ref}
                onChange={onChange}
                value={value}
            />
        );
    }
);

const MyPhoneInput = () => {

    const ref = React.createRef();
    const myTextInput = <ForwardedInput 
        onChange={() => {}}
        value="string"
        ref={ref}
    />;

    return (
        <div>
            <PhoneInput
                    placeholder="Enter phone number"
                    value={"value"}
                    inputComponent={myTextInput}
                    onChange={() => {
                        return;
                    }}
                />
        </div>
    );
};

export default MyPhoneInput;
EN

回答 1

Stack Overflow用户

发布于 2021-05-24 17:01:19

inputComponent={ForwardedInput}

这是可行的。现在我的生活非常忙碌,我没有时间详细解释,但我会尽快回到这个问题上来。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67661806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档