简体   繁体   中英

How can I use formik “arrayHelper” outside of FieldArray?

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM