reactjs - {React Native} 提交后重置 Formik 表单

标签 reactjs react-native formik

提交后,我一直在重置我的表单。

这是我所拥有的

  const PostEvent = () =>{

        //Omitted code for brevity. 

return(
    <Formik
         initialValues ={{user: userName.currentUser.displayName, description: '',datePosted: new Date(), location: '', eventDate: '', title: ''}}
         onSubmit ={(values, {resetForm}) =>{
                 firebase.firestore().collection('PostedFunEvents').add({
                     datePosted: values.datePosted.toDateString(),
                     description: values.description, 
                     eventDate: date.toDateString(),
                     eventTime: date.toTimeString(),
                     location: values.location,
                     title: values.title,
                     user: firebase.auth().currentUser.displayName,
                     userId : firebase.auth().currentUser.uid
                     });
                 resetForm({values:''})
                 }}> 
         {props =>(

         <ScrollView keyboardShouldPersistTaps='always'>
         <DismissKeyBoard>
         <View style={{flex:1}}>
             <View  style={styles.form}><Text  style={styles.text}>Pick a place for the event</Text></View>
             <View style= {{paddingTop:20}}>
                 <GoogleAutocomplete 
                     placeholder = 'Insert place to post about '
                     onPress={(data, details = null) => {
                             {props.values.location = data.description}
                         }}>     
                 </GoogleAutocomplete>
             </View >

                <View style={styles.form}>    

                 <Text style={styles.text}>{userName.currentUser.displayName}</Text>
                 <TextInput
                     placeholder= 'Title of the event'
                     onChangeText = {props.handleChange('title')} 
                     style={styles.text}/>
                 <TextInput
                     placeholder= 'Description (e.g This is located...)'
                     multiline={true}
                     onChangeText = {props.handleChange('description')}
                     values = {props.values.description} 
                     style={styles.textBox}/>

                 <Text  style={styles.text} >Click on the below icons to pick a time and a date for the event</Text>

                 <DateTimeImage onPress={showTimepicker} name = 'alarm'>Time?</DateTimeImage> 
                 <DateTimeImage onPress={showDatepicker} name = 'calendar'>Date?</DateTimeImage> 

                  {show && (<DateTimePicker onChange={onChange} value = {date} mode = {mode}></DateTimePicker>)}

                 <Button onPress={props.handleSubmit}>Submit</Button>

                 </View>    

         </View>       
         </DismissKeyBoard>     
         </ScrollView>
         )}
     </Formik> 
 )
}

这个任务应该很简单,但我似乎无法让它工作。我看了很多教程和SO帖子,他们似乎都有相同的想法,就是将回调重置函数传递给onSubmit <Formik>的 Prop 成分。
任何人都知道我可以尝试什么?

提前致谢。

最佳答案

当您重置表单时,值应该是initialValues 的格式。您可以将initalValues作为对象取出并将其传递给重置表单

const initialValues = {
      user: userName.currentUser.displayName, 
      description: '',
      datePosted: new Date(), 
      location: '', 
      eventDate: '', 
      title: ''
}

..
<Formik
     initialValues ={initialValues}
     onSubmit ={(values, {resetForm}) =>{
             firebase.firestore().collection('PostedFunEvents').add({
                 datePosted: values.datePosted.toDateString(),
                 description: values.description, 
                 eventDate: date.toDateString(),
                 eventTime: date.toTimeString(),
                 location: values.location,
                 title: values.title,
                 user: firebase.auth().currentUser.displayName,
                 userId : firebase.auth().currentUser.uid
                 });
             resetForm({values: initialValues})
             }}> 

已链接 github issue

关于reactjs - {React Native} 提交后重置 Formik 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61949635/

相关文章:

reactjs - 如何在 formik 中使用 React 数字格式

reactjs - Formik:如何从数组设置初始值?

reactjs - 如何在 Yup 异步验证中设置动态错误消息?

javascript - react 选定的对象,无法显示我想要的

javascript - jQuery 未定义,Electron

reactjs - typescript / react : How can a ref be added to a material-ui Box component?

react-native - Expo.FileSystem.downloadAsync不显示下载通知

javascript - 即使在服务器和客户端上设置 header 后,请求的资源上仍存在错误 "No ' Access-Control-Allow-Origin' header

javascript - `Firebase` 包被成功找到。但是,这个包本身指定了一个无法解析的 `main` 模块字段

ios - React-Native 和 xcode 10 - 将 Base SDK 设置为 12.1 但编译为 11