DEV Community

How to handle multiple inputs in React

Debora Galeano on November 29, 2020

I recently had to refactor a React Form when working with multiple inputs and I thought I'd share how I did it. This is how my form looks like: ...
Collapse
 
musawershah1598 profile image
Musawer Shah

Hi this method works fine for multiple input but just with normal input fields. What if their are some booleans and arrays??? E.g In vue.js I have the following data model which change at some point in time, which in that case two way data binding make it way easier for us.

data() {
        return {
            valid: false,
            showDialog: true,
            showFullLoading: false,
            isImage: false,
            product: {
                name_en: "",
                name_ar: "",
                category: "",
                subcategory: "",
                description_en: "",
                description_ar: "",
                meta_title: "",
                meta_description: "",
                meta_keywords: "",
                price: 0.0,
                showSale: false,
                sale: 0.0,
                image: "",
                saleAfterStock: false,
                stock: 10
            },
            images: [
                { image: null, isImage: false, id: shortid.generate() },
                { image: null, isImage: false, id: shortid.generate() },
                { image: null, isImage: false, id: shortid.generate() },
                { image: null, isImage: false, id: shortid.generate() }
            ],
            attributes: [],
            defaultVariants: [],
            dataWeightClass: ["Gram", "Kilo Gram", "Pound"],
            dataDimensionClass: ["Centimeter", "Inch"],
            showAttributeDialog: false,
            sizeGuide: false,
            sizeGuides: [],
            attribute: {
                title_en: "",
                title_ar: "",
                description_en: "",
                description_ar: "",
                image: null,
                isImage: false
            },
            subcategories: [],
            options: [],
            variantsHeaders: [
                { text: "Variant", value: "name" },
                { text: "Price", value: "price" },
                { text: "Stock", value: "quantity" },
                { text: "Visibility", value: "visibility" }
            ],
            defaultVariantId: "",
            defaultPreviewId: ""
        };
    },
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jgifford82 profile image
jgifford82

Thank you so much!! I'm learning about controlled forms, and this has been incredibly helpful!

Collapse
 
donchocska profile image
donchocska

Amazing perofrmance. Very thanks!

Collapse
 
tombohub profile image
tombohub • Edited

very good , but I don't like it. Now you have to maintain the names of the inputs, which is a string. It's a bug waiting to happen.

I suggest using this solution: stackoverflow.com/a/47707719/1079002

btw, do you have solution for nested objects? I need that, thanks!

Collapse
 
sparshcodes profile image
Sparsh Gupta

Hi, just wanted to ask ,
since we are changing the state in handleInputChange
therefore, every time we will type in something the whole component will re-render since the state is changing
so isn't it an expensive approach in respect to performance?

Collapse
 
yahaya_hk profile image
Yahaya Kehinde

Amazing Deborah ! ☺️☺️

Collapse
 
knightwarrior93 profile image
SHAMIM

Thanks for providing with an useful information in a synchronize manner ...

Collapse
 
majidmo49787329 profile image
Majid Mohamed

Amazing perofrmance. Very thanks!

Collapse
 
salvadorbeltrandev profile image
Salvador Beltrán

Thank you so much, I was handling this terrible, but with your handleChange I was able to refactor the entire event! fav*

Collapse
 
abned08 profile image
Aboubakr Ned

Wow, that exactly what I was looking for

Collapse
 
juliojeanfils1 profile image
Julio Jean Fils

Thank you

Collapse
 
moreyogesh profile image
Yogesh More

The spread operator has not used the in-class component. and this what I am looking for thanks for the exact answer :)

Collapse
 
jocanola profile image
Jocanola

Really clean.

Collapse
 
gsingh profile image
G.Singh

Thank you so much!

Collapse
 
omokay profile image
Omoke Chuku

This is beautiful!

Collapse
 
girinuri123 profile image
Giri

Awesome article!

Collapse
 
philjotham14 profile image
PhilJotham14

for example if I wanted to insert two value in input how do I go by that
value={values.position} for example onChange=" {(e)=> this.onChangeInput(e)}; { handleChange }; "

Collapse
 
suhrobmuboraksho profile image
Suhrob Muborakshoev

Thanks for the explanation. Kudos

Collapse
 
krishnacyber profile image
krishna-Tiwari

thanks a lot iam actually looking for it thanks again

Collapse
 
haleemik profile image
haleemik

Can we map the input filed using an array of states. So that we can reduce the code size. Is it possible to do that?

Collapse
 
chhadi profile image
Ch_Hadi

Noice , i like that Deborah

Collapse
 
abhay5855 profile image
Abhay Patil

When using state like this same example I am getting warning - input type is getting uncontrolled
when I use seperate state for all feilds I don't get this error