javascriptreactjsreact-nativemultipartform-data

How to get data from React native FormData Object


I am working on a react native project. I am using FormData to upload my files and input text.

But in form validation, I need to get the Value from the FormData object. I tried formData.get('mykey') but it gives me an error that gets is not a function. Can anyone please tell me how can I do that?

thanks.


Solution

  • You can use FormData#getParts:

    const field = data.getParts().find(item => item.fieldName === 'key'));
    if (field) {
      const value = field.string;
    }
    

    See relevant React Native source code for FormData.