Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use formik "arrayHelper" outside of FieldArray?

Tags:

I trying to create 2 FieldArray containers with arrayHelpers. Each of them working with different object. Obj1 and Obj2 have their arrayHelper and each of them know how to add and remove objects to/from array.

I want to do some kind of toolbar with two buttons "Add Obj1" and "Add Obj2" outside of these two containers. The question is how can I work with arrayHelper outside of these containers? Or could you suggest another flow how can change my array? The problem is if I change my array from parent - I can't work with props.values only with props.obj1 but in this case my changes are not saving.

<FieldArray name="obj1"
                    render={arrayHelpers => {}/>
<FieldArray name="obj2"
                    render={arrayHelpers => {}/>

<div class="toolbar">...buttons</div>

like image 688
Dzmitry Shchepin Avatar asked May 29 '19 20:05

Dzmitry Shchepin


1 Answers

To access arrayHelpers outside of a FieldArray, you can use refs.

I've created a code sandbox, showing how you can do this:

https://codesandbox.io/s/using-refs-to-access-child-methods-cps7w

like image 146
Stafford Rose Avatar answered Sep 29 '22 07:09

Stafford Rose