如何在输入中添加引用?

时间:2019-08-12 10:36:38

标签: reactjs typescript

我正在尝试根据以下文档将ref添加到输入:http://skrinshoter.ru/s/120819/zewG9lEK?a

这是我的代码:

interface Props {
  update?: (file: File[]) => void,
}

const ref = React.createRef();

export const addImage = <P extends object>(WrappedComponent: React.ComponentType<P & Props>, props: Props) => {
  class AddImage extends React.Component<P & Props, State> {

toggleInputCopyImg = () => {

  this.setState(state => ({ visibleCopyImg: !state.visibleCopyImg }))
  if (this.state.visibleCopyImg) {
    props.forwardedRef.current.focus()
  }
}

    render() {
      const { update, forwardedRef } = props
      const { src } = this.state
      const otherProps = update ? { src } : {}
      console.log('our props', props)


      return (

        <Dropzone noClick onDrop={(file) => this.onDrop(file)}>
          {({ getRootProps, getInputProps }) => (

            <Div {...getRootProps()} onClick={this.toggleInputCopyImg}>
              <InputStyled ref={forwardedRef} type="text" placeholder="URL:" />
              <input {...getInputProps()} />
              <WrappedComponent {...this.props} {...otherProps} />
            </Div>

          )}

        </Dropzone>
      )

    }
  };

  return React.forwardRef((props, ref: React.RefObject<HTMLInputElement>) => {
    console.log("ref", ref)
    return <AddImage {...props} forwardedRef={ref} />;
  });
}

但是最后我在行中有错误*类型“ {{forwardedRef:Ref; children?:ReactNode;}”不能分配给类型“ IntrinsicAttributes&IntrinsicClassAttributes&Readonly&Readonly <{children?:ReactNode;}” >”。 forwardedRef属性不存在于IntrinsicAttributes&IntrinsicClassAttributes&Readonly&Readonly <{children?:ReactNode;}>类型。Ts(2322)

,当单击时,未设置专注于输入(

如何解决该错误?

0 个答案:

没有答案