SWEBDEV Telegram 3140
Использование useFieldArray в React

useFieldArray — это хук из библиотеки React Hook Form, помогающий управлять массивами полей в динамических формах.

Пример:
import React from 'react';
import { useForm, useFieldArray, Controller } from 'react-hook-form';

function DynamicForm() {
const { control, handleSubmit } = useForm({ defaultValues: { items: [''] } });
const { fields, append, remove } = useFieldArray({ control, name: 'items' });

return (
<form onSubmit={handleSubmit(console.log)}>
{fields.map((field, index) => (
<div key={field.id}>
<Controller
name={`items[${index}]`}
control={control}
render={({ field }) => <input {...field} />}
/>
<button type="button" onClick={() => remove(index)}>Удалить</button>
</div>
))}
<button type="button" onClick={() => append('')}>Добавить элемент</button>
<input type="submit" />
</form>
);
}

export default DynamicForm;

👉 @sWebDev
👍3



tgoop.com/sWebDev/3140
Create:
Last Update:

Использование useFieldArray в React

useFieldArray — это хук из библиотеки React Hook Form, помогающий управлять массивами полей в динамических формах.

Пример:

import React from 'react';
import { useForm, useFieldArray, Controller } from 'react-hook-form';

function DynamicForm() {
const { control, handleSubmit } = useForm({ defaultValues: { items: [''] } });
const { fields, append, remove } = useFieldArray({ control, name: 'items' });

return (
<form onSubmit={handleSubmit(console.log)}>
{fields.map((field, index) => (
<div key={field.id}>
<Controller
name={`items[${index}]`}
control={control}
render={({ field }) => <input {...field} />}
/>
<button type="button" onClick={() => remove(index)}>Удалить</button>
</div>
))}
<button type="button" onClick={() => append('')}>Добавить элемент</button>
<input type="submit" />
</form>
);
}

export default DynamicForm;

👉 @sWebDev

BY Frontender Libs - обзор библиотек JS / CSS




Share with your friend now:
tgoop.com/sWebDev/3140

View MORE
Open in Telegram


Telegram News

Date: |

Telegram Channels requirements & features Invite up to 200 users from your contacts to join your channel Telegram users themselves will be able to flag and report potentially false content. Hui said the messages, which included urging the disruption of airport operations, were attempts to incite followers to make use of poisonous, corrosive or flammable substances to vandalize police vehicles, and also called on others to make weapons to harm police. You can invite up to 200 people from your contacts to join your channel as the next step. Select the users you want to add and click “Invite.” You can skip this step altogether.
from us


Telegram Frontender Libs - обзор библиотек JS / CSS
FROM American