如何使用 Unform 从另一个文件访问输入引用?

huangapple go评论67阅读模式
英文:

How to access input reference from another file using Unform?

问题

我正在尝试使用以下结构的unform:

Component/inputRadio.ts:

export const TextInputInput: React.FC<TextInputInputProps> = ({ name, ...props }) => {
    const inputRef = useRef<HTMLInputElement>(null);
    const { fieldName, registerField } = useField(name!);

    useEffect(() => {
        registerField({
            name: fieldName,
            ref: inputRef.current,
            path: 'value',
        });
    }, [fieldName, registerField]);
        
    return(
        <InputCss ref={inputRef} className={clsx(InputCss)} {...props} />
    )
}

在另一个文件中,这是我的提交函数:

const Login: React.FC = () => {
    const formRef = useRef<FormHandles>(null);

    const handleSubmit = useCallback(
        async (data: SignInFormData) => { 
          try {    
            console.log(data);
          } catch (err) {    
            console.log(err);
          }
        },
        [],
    );

    return(
        <Form ref={formRef} onSubmit={handleSubmit}>
            <TextInputInput 
                type='text'  
                id="text" 
                name='cpf'
                placeholder='000.000.000-00' 
            />

            <FormatedTextInput 
                type="password" 
                id='password'
                name='password'
                placeholder='********'
            />
            <Button type='submit'>Entrar</Button>
        </Form>
    )
}

export default Login;

我该如何在组件外部操作值?我已经在组件内部使用了useRef和这个“标准”的Unform结构。如何在另一个导入的文件中更改或获取值?

具体来说,我想在点击另一个按钮之前获取一个字段的值,这是在提交函数之前。

我不想使用document.getElementById()

英文:

I'm trying to use unform with the following structure:

Component/inputRadio.ts:

export const TextInputInput: React.FC&lt;TextInputInputProps&gt; = ({ name, ...props }) =&gt; {
    const inputRef = useRef&lt;HTMLInputElement&gt;(null);
    const { fieldName, registerField } = useField(name!);

    useEffect(() =&gt; {
        registerField({
            name: fieldName,
            ref: inputRef.current,
            path: &#39;value&#39;,
        });
    }, [fieldName, registerField]);
        
    return(
        &lt;InputCss ref={inputRef} className={clsx(InputCss)} {...props} /&gt;
    )
}

In another file this is my submit function:

const Login: React.FC = () =&gt; {
const formRef = useRef&lt;FormHandles&gt;(null);

const handleSubmit = useCallback(
    async (data: SignInFormData) =&gt; { 
      try {    
        console.log(data);
      } catch (err) {    
        console.log(err);
      }
    },
    [],
);

return(

        &lt;Form ref={formRef} onSubmit={handleSubmit} &gt;
                    &lt;TextInputInput 
                        type=&#39;text&#39;  
                        id=&quot;text&quot; 
                        name=&#39;cpf&#39;
                        placeholder=&#39;000.000.000-00&#39; 
                    /&gt;

                    &lt;FormatedTextInput 
                        type=&quot;password&quot; 
                        id=&#39;password&#39;
                        name=&#39;password&#39;
                        placeholder=&#39;********&#39;
                    /&gt;
            &lt;Button type=&#39;submit&#39;&gt;Entrar&lt;/Button&gt;
        &lt;/Form&gt;
    )
}

export default Login;

how can I manipulate the value outside the component? I'm already using useRef inside the component with this "standard" Unform structure. How do you change or get the value in another file import?

specifically I want to get one field value when click in another button, this before the submit function.

I dont want to use document.getElementById().

答案1

得分: 0

你可以使用formRef.current.getFieldValue

例如,如果你想获取cpf的值,可以使用formRef.current.getFieldValue('cpf')

如果你想访问所有的值,可以使用formRef.current.getData()

这些也在unform指南中有详细说明。

英文:

You can use formRef.current.getFieldValue.

For example, if you want to get the value of cpf, you can use formRef.current.getFieldValue(&#39;cpf&#39;).

Use formRef.current.getData() if you want to access all values.

These are also documented in the unform guides.

huangapple
  • 本文由 发表于 2023年8月9日 02:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定