{"id":13034,"date":"2025-08-01T11:54:42","date_gmt":"2025-08-01T11:54:42","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13034"},"modified":"2025-08-01T11:54:42","modified_gmt":"2025-08-01T11:54:42","slug":"use-formdata-in-react-native","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react-native\/use-formdata-in-react-native","title":{"rendered":"Use FormData in React Native"},"content":{"rendered":"<p>In React Native, FormData is used when you need to send form-like data, such as file uploads or key-value pairs, typically in a POST request. It behaves similarly to how it works in the browser.<\/p>\n<p>Here&#8217;s a basic example of how to use FormData in React Native:<\/p>\n<h3>Example 1: Sending Text Fields<\/h3>\n<p>Using a functional component with useState and filter method (Recommended for modern apps):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">const sendFormData = async () =&gt; {\r\n  const formData = new FormData();\r\n  formData.append('name', 'John Doe');\r\n  formData.append('email', 'john@example.com');\r\n\r\n  try {\r\n    const response = await fetch('https:\/\/your-api-url.com\/endpoint', {\r\n      method: 'POST',\r\n      body: formData,\r\n      headers: {\r\n        'Content-Type': 'multipart\/form-data',\r\n      },\r\n    });\r\n    const result = await response.json();\r\n    console.log(result);\r\n  } catch (error) {\r\n    console.error('Error:', error);\r\n  }\r\n};\r\n<\/pre>\n<h3>Example 2: Uploading a File (e.g., Image)<\/h3>\n<p>Make sure the file object contains the correct structure:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">const uploadImage = async () =&gt; {\r\n  const formData = new FormData();\r\n\r\n  \/\/ Replace this with your image picker result\r\n  const image = {\r\n    uri: 'file:\/\/\/path\/to\/image.jpg',\r\n    name: 'image.jpg',\r\n    type: 'image\/jpeg',\r\n  };\r\n\r\n  formData.append('photo', image);\r\n  formData.append('userId', '12345'); \/\/ you can send other data too\r\n  try {\r\n    const response = await fetch('https:\/\/your-api-url.com\/upload', {\r\n      method: 'POST',\r\n      body: formData,\r\n      headers: {\r\n        'Content-Type': 'multipart\/form-data',\r\n        \/\/ Add any other headers as needed (e.g., Authorization)\r\n      },\r\n    });\r\n    const result = await response.json();\r\n    console.log(result);\r\n  } catch (error) {\r\n    console.error('Upload Error:', error);\r\n  }\r\n};\r\n<\/pre>\n<h3>Example 3: Sending an Array of Tags and Multiple Images<\/h3>\n<p>If you want to send an array of items using FormData in React Native (e.g., a list of tags, multiple images, etc.), you can do it by appending each item individually with indexed or bracketed keys, depending on what the backend expects.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">const uploadFormWithArray = async () =&gt; {\r\n  const formData = new FormData();\r\n\r\n  \/\/ Append a text field\r\n  formData.append('username', 'jaimin');\r\n\r\n  \/\/ Append an array of tags (as strings)\r\n  const tags = ['react', 'native', 'formdata'];\r\n  tags.forEach((tag, index) =&gt; {\r\n    formData.append(`tags[${index}]`, tag);\r\n  });\r\n\r\n  \/\/ Append an array of images\r\n  const images = [\r\n    {\r\n      uri: 'file:\/\/\/path\/to\/image1.jpg',\r\n      name: 'image1.jpg',\r\n      type: 'image\/jpeg',\r\n    },\r\n    {\r\n      uri: 'file:\/\/\/path\/to\/image2.jpg',\r\n      name: 'image2.jpg',\r\n      type: 'image\/jpeg',\r\n    },\r\n  ];\r\n  images.forEach((image, index) =&gt; {\r\n    formData.append(`photos[${index}]`, image);\r\n  });\r\n\r\n  try {\r\n    const response = await fetch('https:\/\/your-api-url.com\/upload', {\r\n      method: 'POST',\r\n      body: formData,\r\n      headers: {\r\n        \/\/ Content-Type should generally be left out for FormData\r\n        \/\/ 'Content-Type': 'multipart\/form-data',\r\n      },\r\n    });\r\n\r\n    const result = await response.json();\r\n    console.log('Upload successful:', result);\r\n  } catch (error) {\r\n    console.error('Upload error:', error);\r\n  }\r\n};\r\n<\/pre>\n<h2>Notes:<\/h2>\n<ul>\n<li>The Content-Type: multipart\/form-data header is sometimes best left unset because fetch will automatically set it with a boundary.<\/li>\n<li>Ensure the uri is in correct format (file:\/\/ for local files on Android\/iOS).<\/li>\n<li>Use libraries like:<br \/>\n&#8211; React-native-image-picker<br \/>\n&#8211; Expo-image-picker<br \/>\n&#8211; react-native-document-picker<\/li>\n<li>to select files from the device.<\/li>\n<\/ul>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/react-native-hooks-to-build-app\" target=\"_blank\">React Native Hooks<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In React Native, FormData is used when you need to send form-like data, such as file uploads or key-value pairs, typically in a POST request. It behaves similarly to how it works in the browser. Here&#8217;s a basic example of how to use FormData in React Native: Example 1: Sending Text Fields Using a functional [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13035,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-13034","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-native"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13034"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13034"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13034\/revisions"}],"predecessor-version":[{"id":13036,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13034\/revisions\/13036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13035"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}