Replies: 2 comments 1 reply
-
Hi did you find any solution to it? Currently looking for some alternate options. Do share if you have any. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Here's how you do this via a re-render: Let's say you have three links that where you want to edit the thing inside of a single modal / form. First we're going to set some state on our App const App = () => {
const [showModal, setShowModal] = useState(false)
const [thing, setThing] = useState('')
...
} In our app, here's our three links that open the modal and set the thing: <a href="#" onClick={() => { setShowModal(true); setThing('foo')}}>
<a href="#" onClick={() => { setShowModal(true); setThing('bar')}}>
<a href="#" onClick={() => { setShowModal(true); setThing('baz')}}> Also in the app, let's add the modal where we pass down <MyCoolModal setThing={setThing} setShowModal={setShowModal} showModal={showModal} /> In your modal component, pass const MyCoolModal = ({setThing, setShowModal, showModal}) => (
<Modal show={showModal}>
<ParentForm thing={thing}>
<Modal.Footer>
<Button onClick={ () => { setThing(''); setShowModal(false) } }>Close</Button>
</Modal.Footer>
</Modal>
) Inside of const ParentForm = ({ thing }) => {
const initialFormValues = {thing: thing}
return (
<Formik
enableReinitialize
initialValues={{ initialFormValues }}
... Now when you click the close button on the modal, it will set the thing to empty and re-initialize the form once you click a different link. |
Beta Was this translation helpful? Give feedback.
-
I cant reset my form on submit or modal close event using formik. I tried using
resetForm({});
,resetForm(initialValues),resetForm('')
nothing works for me.RestaurantDetails.js
setting initial values
render part of formik
Beta Was this translation helpful? Give feedback.
All reactions