0

im trying to set the state of something and its not working component did mount is set to async but when trying the same on my method it said async/await does nothing. this is in a jsx page. I know set state is asynchronous but i thought if componentdidmount was async then all functions called by it are async.

   constructor(props) {
    super(props);
    // states
    this.state = { newObject:"",supra:"",newObject:"",itemName:"",options:"",searchBar:[{locations:["country","region","area","city","neighborhood"]}]};
    this.createNewPage=this.createNewPage.bind(this);
}

...

 //to search by location.
        async componentDidMount(){
            //set possible options
            let line=new URLSearchParams(window.location.pathname).get("searchCriteria");
             //ignore and skip to else
            if(line!=null){
                let values=line.split(",");
                let newPath="";
                let possibleOptions=[];
                values.map((value,index)=>
                {
                    if(index!=0){
                        newPath+="/"+value;
                    }
                });
          //gets objects form all location type based on new path
            axios.get(
                    'http://localhost:8080/'+"country"+newPath)
                       .then(res => {res.map(e=>{ let keyCode='<MenuWrapper id='+e.id+' name='+e.value+' supra='+null +'/>';
                        possibleOptions.concat(keyCode);});
                        //fix content
                       
                     }).catch(
                     function (error) {
                    });
                axios.get(
                    'http://localhost:8080/'+"region"+newPath)
                       .then(res => {res.map(e=>{ let keyCode='<MenuWrapper id='+e.id+' name='+e.value+' supra='+e.country +'/>';
                        possibleOptions.concat(keyCode);});
                     }).catch(
                     function (error) {
                    });

                axios.get(
                    'http://localhost:8080/'+"area"+newPath)
                       .then(res => {res.map(e=>{ let keyCode='<MenuWrapper id='+e.id+' name='+e.value+' supra='+e.region +'/>';
                       possibleOptions.concat(keyCode);});
                    }).catch(
                     function (error) {
                    });

                    axios.get(
                        'http://localhost:8080/'+"city"+newPath)
                           .then(res => {res.map(e=>{ let keyCode='<MenuWrapper id='+e.id+' name='+e.value+' supra='+e.region +'/>';
                           possibleOptions.concat(keyCode);});
                        }).catch(
                         function (error) {
                        });

                        axios.get(
                          'http://localhost:8080/'+"neighborhood"+newPath)
                             .then(res => {res.map(e=>{ let keyCode='<MenuWrapper id='+e.id+' name='+e.value+' supra='+e.region +'/>';
                             possibleOptions.concat(keyCode);});
                          }).catch(
                           function (error) {
                          });
           
            this.setState({options:possibleOptions});}
            else{
                let id=new URLSearchParams(window.location.pathname).get("id");
                let category=new URLSearchParams(window.location.pathname).get("category");
                let supra="";
                //will set it to country
                if(category===null){
                     await this.setState({type:"country"},() => {
                        console.log(this.state.type);
                      });

                      

                }
                else{
                    this.setState({type:category},() => {
                        console.log(this.state.type)});
                }
                //if id exists get object
                if(id){
                   
                   axios.get(
                    'http://localhost:8080/'+category+'/'+id)
                       .then(res => {
                       this.setState({supra:res});
                     
                     }).catch(
                     function (error) {
                    }); 
                }
                
                //function problem is stuck on.
                 this.createNewPage();

            }
        }
        //content for new object
       createNewPage(){
         
        let keyCode="";
        //will go into this one.
            if(this.state.type==="country"){
                
                keyCode="<div> <b>create a new country: </b><country id=null /></div>";
                //new object does not initialise by  keyCode 
                //
                //

               this.setState({newObject: keyCode }, () => {
                    console.log(this.state.newObject);
                });

                  let x=this.state.newObject;
            }
            else if(this.state.type==="region"){
                keyCode="<div> <b>create a new region:</b><region id=newItem country="+this.state.supra+"/></div>";
              this.setState({newObject:keyCode},() => {
                    console.log(this.state.newObject);
                  });
            }
            else if(this.state.type==="area"){
                keyCode="<div> <b>create a new area: </b><area id=newItem region="+this.state.supra+"/></div>";
                 this.setState({newObject:keyCode},() => {
                    console.log(this.state.newObject);
                  });
            }
            else if(this.state.type==="city"){
                keyCode="<div> <b>create a new city: </b><city id=newItem region="+this.state.supra+"/> </div>";
                 this.setState({newObject:keyCode},() => {
                    console.log(this.state.newObject);
                  });}
            else if(this.state.type==="neighborhood"){
                keyCode="<div> <b>create a new: {this.state.supra}</b><neighborhood id=newItem city="+this.state.supra+"/> </div>";
                this.setState({newObject:keyCode},() => {
                    console.log(this.state.newObject);
                  });
            }
        }
  • if you want to code waits till a function is done use `await` before it's call `await asyncFunction()` but it does not work on `setState` so you can not write `awate setState()` – abolfazl shamsollahi Oct 24 '22 at 10:31
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Konrad Oct 24 '22 at 10:39

1 Answers1

0

sorry guys it does exactly what i wanted from the start i just didn't have any sleep and was confused why a component called in the string wasn't calling a separate issue, thank you for your help.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '22 at 10:47