Skip to content

Instantly share code, notes, and snippets.

@dayhaysoos
Created January 10, 2022 22:52
Show Gist options
  • Save dayhaysoos/c64ebef70480914a0bbdb991b1426c64 to your computer and use it in GitHub Desktop.
Save dayhaysoos/c64ebef70480914a0bbdb991b1426c64 to your computer and use it in GitHub Desktop.
import React, { useState, useRef, useEffect } from 'react'
import { Box, Flex, Button, Text, Input } from 'theme-ui'
import { useFieldArray, useFormContext } from 'react-hook-form'
function ContributionInput() {
const { trigger, control, register } = useFormContext()
const { fields, append, remove } = useFieldArray({
name: 'contributions',
control
})
return (
<>
{fields.map((field, index) => {
return (
<div key={field.id}>
<input
{...register(`contributions.${index}.value`)}
defaultValue=""
/>
<button
type="button"
onClick={() => {
remove(index)
trigger()
}}
>
Remove question {field.id}
</button>
</div>
)
})}
<button type="button" onClick={() => append({ text: '' })}>
Add question
</button>
</>
)
}
export default ContributionInput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment