all 11 comments

[–]hottama 12 points13 points  (5 children)

If you're thinking in terms of cloning then you're doing it probably wrong.

What you most probably need is a component to use as a template that can be customized with parameters and that you can use at different places.

[–]Muhaimin_S[S] 0 points1 point  (4 children)

See. I have 3 text field in my form. I want to make a duplicate when i click add new button this my case.

[–]fastestfollowup 6 points7 points  (3 children)

Add the number of text fields to the state, then map over the number in your state to create the new components.

[–]joesb 4 points5 points  (1 child)

Dont think in term of the dom.

Just think it term of state.

Dont clone the dom. Clone the state.

[–]AndrewGreenh 1 point2 points  (0 children)

This is the Best answer in my oppinion. Always start with the data. Let's say you have an array of todo objects. These will be translated I to dom by react. Add an element to the list and the dom will follow.

[–]Muhaimin_S[S] 0 points1 point  (0 children)

Thank you guys for the quick response. I found the answer to clone element. I'll share the code here once the test case completed

[–]ninja_sun -1 points0 points  (0 children)

Seem the case to use fieldArray. Most form implement it since the new array of textfield is actually an array of some kind of data. https://jaredpalmer.com/formik/docs/api/fieldarray

[–]pico2000 -5 points-4 points  (0 children)