Code Monkey home page Code Monkey logo

Comments (8)

ariesjia avatar ariesjia commented on August 16, 2024 1

谢谢,我看看这个issue

from react-use-form.

ariesjia avatar ariesjia commented on August 16, 2024

谢谢,

例如现在有一个表单

const [form, field]  = useForm({})
const nameField = field("name")

getValue

这个需求目前可以通过
nameField.value 来获取到name这个表单项的value
form.value 来获取整个form的value

setError

这个我看一下有没可能可以支持到,主要是如何清除set的error,比较难找到一个通用的模式

不知通过 自定义的validator 或者 组件内部error state 来呈现,能否满足到需求

from react-use-form.

sessionboy avatar sessionboy commented on August 16, 2024
getValue
这个需求目前可以通过
nameField.value 来获取到name这个表单项的value
form.value 来获取整个form的value

这个在外部可以,但是在自定义验证函数里面就获取不到,我试过了。

代码如下:

// 密码
<input 
   type="password" 
   {...field("password", {
        rules: [{
           type: "string", 
           required: true,
           min:8,
           max:16,
           message:"8-16位字符"
       }]
   })}
/>

给确认密码字段的input添加自定义验证函数,但是在这个验证函数里面无法获取到最新的password值,获取到的始终是初始值。

// 确认密码
<input 
   type="password" 
   {...field("apassword", {
        rules: [{
           type: "string", 
           validator: (rule, value, callback)=>{
               const password = form.value.password;  //  得到的是初始值"",无法得到最新的值
               if(value != password){
                      callback("两次输入的密码不一致!");
                }
               callback();
           }
       }]
   })}
/>

这可能是个bug,你调试下。

如果有个getValue方法即时获取单个值,也能解决这个问题。

from react-use-form.

ariesjia avatar ariesjia commented on August 16, 2024

谢谢,反馈的这个问题
是因为验证器经过了memoize导致的
考虑到性能,按照你的思路,新版本增加了getValue方法,

demo:
https://github.com/ariesjia/react-use-form/blob/master/src/__stories__/6-custom-validate.story.tsx

from react-use-form.

sessionboy avatar sessionboy commented on August 16, 2024

棒👍!
刚试了下,好像还是有问题
form.getValue().password获取到的始终是password的第一个字符,不管你的值是多少个字符,它都只能获取到值的第一个字符。

from react-use-form.

ariesjia avatar ariesjia commented on August 16, 2024

先暂时去掉memoize,后面考虑再如何解决,
测试应该可以了

https://ariesjia.github.io/react-use-form/?path=/story/rc-use-form--custom-validate-value

from react-use-form.

sessionboy avatar sessionboy commented on August 16, 2024

刚又试了,可以了👍。

如果不是复杂的表单,性能问题其实不大。

如果是复杂的表单需求,可能就要用formik这类的比较大的表单库了。

或者你可以参考下formik,还有antd的form的思路。

还有一个叫formal的表单库也不错,这个性能也挺好,单纯的使用useMemo, useCallback来做优化。但它用的是yup来做验证的。

还有个思路就是不可变数据,你可以用immer来做不可变数据优化。

目前基于react hooks 的比较成熟的表单库还没有。

期待你这个库越来越成熟。

from react-use-form.

ariesjia avatar ariesjia commented on August 16, 2024

谢谢

from react-use-form.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.