Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在Formik内部创建具有条件的变量_Reactjs_Formik - Fatal编程技术网

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}
)
}