0

i use react-native-fetch-blob to send images to spring server. but when i send state it didn't work. the first code is working well but second code is not working. i couldn't find problem between the first and second code.

if (this.state.uri.didCancel) {
            }
            else if (this.state.uri.error) {
            }
            else if (this.state.uri.customButton) {
            }
            else {
                let source = { uri: this.state.uri }
                RNFetchBlob.fetch('POST',  'https://a89ab0aa.ngrok.io/spring01/up', {
                    'Content-Type': 'multipart/form-data',
                }, 
                [{ name: 'file', filename: 'abc.jpg', data: RNFetchBlob.wrap(this.state.uri) }]
                ).then((res) => {
                    })
                    .catch((err) => {
                        // error handling ..
                    })
                this.setState({
                    imagereturn: source
                });
            }
  }


it works well


newimages = [...this.state.images, { name: 'file', fileName: 'abc.jpg', data: RNFetchBlob.wrap(this.state.uri) } ];
  this.setState({
    images:newimages,
  })
if (this.state.uri.didCancel) {
            }
            else if (this.state.uri.error) {
            }
            else if (this.state.uri.customButton) {
            }
            else {
                let source = { uri: this.state.uri }
                RNFetchBlob.fetch('POST',  'https://a89ab0aa.ngrok.io/spring01/up', {
                    'Content-Type': 'multipart/form-data',
                }, 
                this.state.images
                ).then((res) => {
                    })
                    .catch((err) => {
                        // error handling ..
                    })
                this.setState({
                    imagereturn: source
                });
            }
  }

it doesnt work

한동준
  • 3
  • 2
  • does it work if you remove the spread operator? i.e., `...this.state.images`? Also, the call to `this.setState` is asynchronous so it may not actually be set by the time the call to `fetch` is made – Mike M Jul 23 '19 at 11:47
  • thanks. i solved it by using push and this.setState. – 한동준 Jul 24 '19 at 05:22

0 Answers0