登录本机后无法刷新应用

时间:2019-07-10 07:10:51

标签: reactjs react-native

我在我的React Native应用程序中执行登录应用程序功能,但是我遇到一个问题,即我无法获取新的用户数据,这要求我杀死我的应用程序并再次打开以获取新数据。我正在使用async storage存储我的数据,请问有任何人可以帮助登录后自动更新数据吗?Native是我的代码...

这是我执行登录功能的axios通话...

 axios.post('https://.../wp-json/api/v1/user/do_login', {
        username: username,
        password: password
          })
          .then( async (response) => {
            if(response.data.type == "success"){
               this.props.navigation.navigate('Home');   
               await AsyncStorage.setItem('full_name', response.data.profile.pmeta.full_name);
               await AsyncStorage.setItem('user_type', response.data.profile.pmeta.user_type);
               await AsyncStorage.setItem('profile_img', response.data.profile.pmeta.profile_img);
               await AsyncStorage.setItem('profileBanner',response.data.profile.pmeta.banner_img)
               await AsyncStorage.setItem('profileType', response.data.type);
               await AsyncStorage.setItem('projectUid' , response.data.profile.umeta.id);
               await AsyncStorage.setItem('projectProfileId', response.data.profile.umeta.profile_id);


              //  NativeModules.DevSettings.reload();
            }else if(response.data.type == "error"){
              alert("Incorrect Detail");
            }
        })
          .catch((error) => {
            console.log(error);
          });

问题是我没有在主屏幕上获取新的用户数据,这需要杀死应用程序,然后再次启动以获取新的用户数据....但是我希望登录后自动获取新的用户数据。请帮助

在主屏幕上,我正在获取数据,这里是代码...

export default class home extends React.Component {

  state = {
    data: [] , 
    default_color:'#fff',
    storedValue:'',
    storedType:'',
    profileImg:'',
    type:'',
    id:'',
    Pid:'',
  };


  componentWillMount() {
    this.getUser();
  }

  fetchData = async () => {
    const response = await fetch(
      BaseUrl + "list/get_categories"
    );
    const json = await response.json();
    this.setState({ data: json });
  };
  fetchFreelancerData = async () => {
    const Pid = await AsyncStorage.getItem('projectProfileId');
    console.log(Pid);
    const response = await fetch(
        BaseUrl + "listing/get_freelancers?listing_type=featured&profile_id=" + Pid 
    );
    const json = await response.json();
    this.setState({ fetchFreelancer: json });
  };
  fetchLatestPostedJobs = async () => {
    const Pid = await AsyncStorage.getItem('projectProfileId');
    const response = await fetch(
      BaseUrl + "listing/get_jobs?listing_type=latest"
    );
    const json = await response.json();
    this.setState({ fetchJobs: json });
  };
  _onPressButton =()=>{
  console.log('Hello');
  }
  _refresh =()=> {
    return new Promise((resolve) => {
      setTimeout(()=>{resolve()}, 2000 )
    });
  }

  getUser = async() =>{
    try {
      const storedValue = await AsyncStorage.getItem('full_name');
      const storedType = await AsyncStorage.getItem('user_type');
      const profileImg= await AsyncStorage.getItem('profile_img');
      const type = await AsyncStorage.getItem('profileType');
      const id = await AsyncStorage.getItem('projectUid');
      const Pid = await AsyncStorage.getItem('projectProfileId');
  //  console.log(storedValue ,storedType, profileImg  ,type , id);
      if(storedValue !== null){
         this.setState({storedValue})
      }else{
        // alert('something wrong')
      }


      if(storedType !== null){
        this.setState({storedType})
     }else{
      //  alert('something wrong')
     }

     if(profileImg !== null){
      this.setState({profileImg})
    }else{
      //  alert('something wrong')
    }

    if(type !== null){
      this.setState({type})
    }else{
      //  alert('something wrong')
    }
    if(id !== null){
      this.setState({id})
    }else{
    //  alert('something wrong')
    }
    if(Pid !== null){
      this.setState({Pid})
    }else{
    //  alert('something wrong')
    }
    this.fetchData();
    this.fetchFreelancerData();
    this.fetchLatestPostedJobs();

    } catch (error) {
      // Error saving data
      // alert(error)
    }
  }

0 个答案:

没有答案