在React Native中调用webService后,Component不会更新该值

时间:2017-08-23 11:19:15

标签: reactjs react-native react-native-android react-native-ios react-native-0.46

我在React Native中调用了GET webService,它得到了成功的响应。但我想在Component中设置此响应。它的意思是根据响应组件不更新。看我的代码。

获取请求:

 makeRemoteRequest = () => {
   this.setState({ loading: true });
   fetch('http://jsonstub.com/ws/pendingInvoices', {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'JsonStub-User-Key': 'daf0e17a-5951-49e0-8d32-4cb4bb804577',
        'JsonStub-Project-Key': '4e70b1a8-12d0-4fa5-8c34-a99b666bd073',
      }
    })
     .then(res => res.json())
     .then(res => {

       console.log('Data Is : ' ,res);
       this.setState({
         text : res,
         customData : res,
         error: res.error || null,
         loading: false,
         refreshing: false
       });
     })
     .catch(error => {
       console.log('error Is : ' ,error);
       this.setState({ error, loading: false });
     });
 };

服务致电:

 componentDidMount() {
   this.makeRemoteRequest();
 }

想要更新文字和手风琴

render(){
       const { navigate } = this.props.navigation;
        return (
          <View style = {styles.scrollSty}>
               <Accordion
                  sections={this.state.customData}
                  renderHeader={this._renderHeader.bind(this)}
                  renderContent={this._renderContent.bind(this)}
                />
              <View><Text style = {{color : 'white'}}>{this.state.text}</Text></View>

         </View>
        );
     }
    }

2 个答案:

答案 0 :(得分:0)

雅。最后获得解决方案:我们可以使用两种方式更新组件。

  1. 强制更新:设置值后调用该函数。

      this.setState({
         customData: customData,
         ...
       });
       this.forceUpdate()
    
  2. 调用shouldComponentUpdate:如果不调用则不更新。

      shouldComponentUpdate(nextProps, nextState) {
        return true;
      }
    

答案 1 :(得分:-2)

我假设您需要在组件的构造函数中绑定您的using System; using System.IO; namespace DropboxDemo { class Program { private static string lookupDirectory = @"E:\Source"; private static string backupDirectory = @"C:\Users\SIMANT\Dropbox \Destination"; static void Main(string[] args) { Console.WriteLine("Please wait while copying files."); CopySourceFilesToDestination(lookupDirectory); Console.WriteLine("Please wait while synchronizing files."); SynchronizeSourceAndDestination(backupDirectory); Console.ReadLine(); } public static void SynchronizeSourceAndDestination(string dir) { foreach (string file in Directory.GetFiles(dir)) { string destFilePath = file.Replace(backupDirectory, lookupDirectory); if (!File.Exists(destFilePath)) { // Delete file from Backup File.Delete(file); } } foreach (string directory in Directory.GetDirectories(dir)) { string destinationDirectory = directory.Replace(backupDirectory, lookupDirectory); if (!Directory.Exists(destinationDirectory)) { Directory.Delete(directory, true); continue; } SynchronizeSourceAndDestination(directory); } } public static void CopySourceFilesToDestination(string dir) { foreach (string file in Directory.GetFiles(dir)) { string destFilePath = file.Replace(lookupDirectory, backupDirectory); if (!File.Exists(destFilePath)) { File.Copy(file, destFilePath); } else { // Override the existing file File.Copy(file, destFilePath, true); } } foreach (string directory in Directory.GetDirectories(dir)) { //Create directory if not present in the destination string destinationDirectory = directory.Replace(lookupDirectory, backupDirectory); if (!Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } CopySourceFilesToDestination(directory); } } } } 方法。

makeRemoteRequest