我试图使用Formik + Yup编写一个用于模式验证的react本机应用程序,并且在提取max验证中通过的常量时遇到了一些困难。
让我们想象一下以下模式:
const message = string()
.max(30)
.default(1)在中,假设是自定义的TextInput,我希望获得常量30,以便将其传递给maxLength支柱:
<TextInput maxLength={max} {...otherProps} />(当然,模式是简化的)
有人知道这是否可行吗?如何深入研究yup模式来访问该常量并将其传递给我的TextInput
发布于 2019-09-04 13:20:02
您可以将一个function传递给validationSchema支柱,所以基本上您的yup模式是,
const validationSchema = value => Yup.object().shape({
name : Yup.string().max(values.max).default(1)
});在<Formik/>组件级呈现方法中,
const { max } = this.state;
<Formik
validationSchema={()=> validationSchema({max})
...>
<TextInput maxLength={max} {...otherProps} />
</Formik>https://stackoverflow.com/questions/57788278
复制相似问题