Skip to content

Instantly share code, notes, and snippets.

@YounglanHong
Last active September 28, 2020 08:02
Show Gist options
  • Save YounglanHong/1e9a12e9acf64035ebf4c7a666186fb1 to your computer and use it in GitHub Desktop.
Save YounglanHong/1e9a12e9acf64035ebf4c7a666186fb1 to your computer and use it in GitHub Desktop.
//* Formik props
const initialValues = {
decks: [""], // --- 1️⃣
};
const onSubmit = (values, actions) => {
registerDeck(values.decks);
getDeck();
// 입력창 초기화
actions.resetForm({
values: {
decks: [""],
},
});
};
<Formik initialValues={initialValues} onSubmit={onSubmit}>
<Form>
<FieldArray name="decks">
{(fieldArrayProps) => {
const { push, remove, form } = fieldArrayProps;
const { decks } = form.values;
return (
<div>
{decks &&
decks.map((deck, index) => (
<Field
name={`decks[${index}]`} {/* --- 2️⃣ */}
placeholder="deck name"
/>
<AddIcon onClick={() => push("")} />
<RemoveIcon onClick={() => remove(index)} />
))}
</div>
);
}}
</FieldArray>
</Form>
</Formik>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment