Comments (1)
I solved it as follow:
/**
* @public
* @description
* Unfortunately, fetch doen't support progress. But we can use xhr which support progress.
*
* I think this way using two ways which are fetch and futch in networking is not pretty.
* So I overwrite fetch.
*
* ```javascript
* import futch from './src/api';
* const originalFetch = fetch
* global.fetch = (url, opts) => {
* console.log(opts.onProgress)
* if (opts.onProgress && typeof opts.onProgress === 'function') {
* return futch(url, opts, opts.onProgress)
* } return originalFetch(url, opts)
* }
* export default class photoUploadTest extends Component {
* ...
* }
* ```
*
* If you add this in your top file like `index.ios.js`, you can use fetch with progress.
*
* ```javascript
* fetch(url + '/array', {
* method: 'post',
* body: data,
* onProgress: (e) => {
* const progress = e.loaded / e.total;
* console.log(progress);
* this.setState({
* progress,
* });
* }
* }).then((res) => console.log(res), (e) => console.log(e))
* ```
*
* To overwrite fetch:
*
* ```javascript
* import futch from './src/api';
* const originalFetch = fetch
* global.fetch = (url, opts) => {
* console.log(opts.onProgress)
* if (opts.onProgress && typeof opts.onProgress === 'function') {
* return futch(url, opts, opts.onProgress);
* }
* return originalFetch(url, opts);
* }
* ```
*
* @param {string} url - the url to futch to
* @param {object} options - futch options (like fetch options)
* @param {function} onProgress - the function to be called on progress event
* @returns {Promise<unknown>} - the pending request
* @example
* import futch from './futch';
*
* const data = new FormData();
* data.append('name', 'testName');
* data.append('photo', {
* uri: source.uri,
* type: 'image/jpeg',
* name: 'testPhotoName'
* });
*
*
* futch(url, {
* method: 'post',
* body: data
* }, (progressEvent) => {
* const progress = progressEvent.loaded / progressEvent.total;
* console.log(progress);
* }).then((res) => console.log(res), (err) => console.log(err))
*/
const futch = (url, options = {}, onProgress) => new Promise((res, rej) => {
if (!options.headers) {
options.headers = {};
}
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open(options.method || 'GET', url);
if (options.headers instanceof Headers) {
for (const pair of options.headers.entries()) {
xhr.setRequestHeader(pair[0], pair[1]);
}
} else {
Object.keys(options.headers).forEach((key) => {
xhr.setRequestHeader(key, options.headers[key]);
});
}
xhr.onload = (e) => res(e.target);
xhr.onerror = rej;
if (xhr.upload && onProgress) {
xhr.upload.onprogress = onProgress;
}
xhr.send(options.body);
});
export default futch;
from react-native-tips.
Related Issues (8)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-tips.