【问题标题】:{React Native} Resetting Formik form after submit{React Native} 提交后重置 Formik 表单
【发布时间】:2020-05-22 07:14:45
【问题描述】:

我在提交后无法重置表单。

这就是我所拥有的

  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帖子,他们似乎都有一个相同的想法,就是将回调重置函数传递给&lt;Formik&gt;组件的onSubmit prop。 有人知道我可以尝试什么吗?

提前致谢。

【问题讨论】:

    标签: reactjs react-native formik


    【解决方案1】:

    当您重置表单时,值应该是 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

    【讨论】:

    • 感谢您抽出宝贵时间提供帮助。我按照您上面的建议做了,但不幸的是,我仍然没有在表单中重置我的值。我想知道&lt;GoogleAutocomplete&gt; 组件是否在搞乱它?
    • 描述字段是否也不会重置
    • 是的,没有任何重置。我故意尝试在描述中使用'values = {props.values.description}'来检查值的呈现,但它根本没有重置。
    • 我在这里用 react 做了一个示例演示,它可以工作 codesandbox.io/s/formik-example-sw5x2?file=/index.js
    • 您的描述字段的道具不正确。应该是value= {props.values.description} 。这就是为什么它没有被清除。其他字段也需要 value 属性才能使重置生效
    猜你喜欢
    • 2019-06-06
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多