0

Problem Statement:

I need to get the input data of all the fields in either Container Component or a Form Component.

Details:

There are 4 to 5 different form Components, each using some common fields in a section. They are extracted into different components and are imported in the top level form components for re-usability purposes. These extracted components have multiple input fields (between 7 to 10 fields each). Here is how the structure of my components looks like,

- Container Component 
-- Form Component A (Contains the submission click handler)
--- InputFieldTypeA Component
--- InputFieldTypeB Component
--- ...

-- Form Component B (Contains the submission click handler)
--- InputFieldTypeA Component
--- InputFieldTypeB Component
--- ...

-- Form Component C (Contains the submission click handler)
--- InputFieldTypeA Component
--- InputFieldTypeB Component
--- ...

I have tried this link but since I have too many input fields, it doesnt make sense to have so many callback handlers (which will be required in FormA, FormB and C components). Other solution was to use a ref given in the same link but I read somewhere that is not the right use of refs and so I gave up on that idea. state cannot be used either in any of these child components (InputFieldTypeA, InputFieldTypeB] etc because we need to lift the state up to the container component.

What is the best solution/strategy in this situation? What is the right way of handling such deeply nested components?

11
  • Are you using Redux? You could have them all in the store and your components could just pick what they need. Commented Sep 4, 2020 at 16:57
  • Would it be a preferred way of doing it? I have to work on adding redux. Haven't gotten their yet. Commented Sep 4, 2020 at 17:04
  • You'd have to answer for yourself if it's preferred. What it will let you do though is store the data in one place that all of your components have equal access to. Commented Sep 4, 2020 at 17:08
  • But then how will you go about testing the components? Commented Sep 4, 2020 at 17:30
  • Unit tests? There are plenty of resources and articles outlining how to test with Redux in the picture. Commented Sep 4, 2020 at 17:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.