Reactjs 在Formik内部创建具有条件的变量
我有下一个代码:Reactjs 在Formik内部创建具有条件的变量,reactjs,formik,Reactjs,Formik,我有下一个代码: export const FormComponent = () => { ... return ( ... <Formik validationSchema={nameEmailValidationSchema} initialValues={{ name: '', email: '' }} onSubmit={(values) => {console.log(values)}}
export const FormComponent = () => {
...
return (
...
<Formik
validationSchema={nameEmailValidationSchema}
initialValues={{ name: '', email: '' }}
onSubmit={(values) => {console.log(values)}}
>
{({
handleChange,
handleBlur,
handleSubmit,
values,
touched,
errors,
isValid = false,
}) => (
...
{isValid && values.name && values.email ? (
<View style={{ paddingTop: 30 }}>
<AppButtonComponent
title="Save"
backgroundColor={Colors.primaryColor}
color={Colors.white}
onPress={handleSubmit}
/>
</View>
) : null}
....
)}
</Formik>
)
}
export const FormComponent=()=>{
...
返回(
...
{console.log(值)}
>
{({
handleChange,
车把,
手推,
价值观
感动的,
错误,
isValid=false,
}) => (
...
{isValid&&values.name&&values.email(
):null}
....
)}
)
}
如何使用条件isValid&&values.name&&values.email
创建变量,例如let showButton=isValid&&values.name&&values.email代码>并将其粘贴到代码中,而不是条件中。
非常感谢您的帮助 例如,在arrow函数中,您可以用多种方式编写它
const add = (a, b) => a + b
// or
const add = (a, b) => (a + b)
此函数立即返回a+b
的结果。但是你不需要缩短它。你还是可以的
const add = (a, b) => {
return a + b;
}
所以不是
<Formik ...yourFormikProps...>
{({
handleChange,
handleBlur,
...
values,
isValid = false,
}) => (
<YourView>....</YourView>
)
</Formik>
{({
handleChange,
车把,
...
价值观
isValid=false,
}) => (
....
)
你能行
<Formik ...yourFormikProps...>
{({
handleChange,
handleBlur,
...
values,
isValid = false,
}) => {
const showButton = isValid && values.name && values.email;
return (
<YourView>
{showButton ? <Button>Save</Button> : null}
</YourView>
)
}
</Formik>
{({
handleChange,
车把,
...
价值观
isValid=false,
}) => {
const showButton=isValid&&values.name&&values.email;
返回(
{showButton?保存:null}
)
}