まだRNで学習中...スマートフォンのブラウザでWebページを開く前に、サーバーから特定のデータfetch()
を取得するためにreact-nativeで使用しようとしています。ここに私が書いたものがあります:
openLink = () => { //Communicate to the server to get an unique key_id
this.state = {urlKey: 'text'}; //Initial state
var params = {
// Some params send by POST to authenticate the request...
};
var formData = new FormData();
for (var k in params) {
formData.append(k, params[k]);
}
fetch(Constants.URL.root+"mobile/authorize_view", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({urlKey:responseJson.document_key}); //Getting the response, and changing the initial state (was 'text' previously)
})
.done();
var urlString = Constants.URL.upload + '/' + this.state.urlKey; // !!Problem : opening in browser with this.state.urlKey = text, and not document_key!!
Linking.canOpenURL(urlString).then(supported => {
if (supported) {
Linking.openURL(urlString);
} else {
console.log('Don\'t know how to open URI: ' + this.props.url);
}
});
}
実際には、ご覧のとおり、サーバーに特定のキーを要求します( JSON オブジェクトで返されるurlKey : responseJson.document_key)。
この生成された document_key をデータベースに配置したため、サーバー側ですべてが正常に実行されており、正しく配置されていることがわかります。
問題は React-native 部分にあります。ブラウザは、関数がサーバーから送信された document_key に変換されるべき初期状態であるWeb ページthis.state.urlKey
を開きます...何が欠けていますか?**text**
fetch