0

I used this code to take a String from Firebase database and the data is separated in a various string which I later store in an array but the main process keeps running and calls the return method before the value gets the data I want.

class PromoActiva extends Component {

  constructor(props) {
        super(props);
        this.state={
          arrayActual:[]
        }

      var recibirArray;
      var user = firebase.auth().currentUser;
      var refDB=ref.child("adan1995a@gmail-com"+"/SlideActual");
      refDB.on('value', snapshot=> {
        recibirArray=snapshot.val().slideActual;
      var StringN="";
      var ArrayFg=[];
      for (var i = 0; i < recibirArray.length; i++) {
        if(recibirArray[i] =='~'){

          for (var j = i; j < recibirArray.length; j++) {
            if(recibirArray[j]!='~'){
              StringN += recibirArray.substring(j,j+1);
            }
            else if (recibirArray[j]=='~'&&j!=0||recibirArray[j+1]===null&&j!=0) {
              ArrayFg.push(StringN);
              StringN="";

            }
          }

        }
      }
      this.setState({
        arrayActual:this.ArrayFg});
      }  );
    }

and when I try to access state.arrayActual, is undefined because the asynchronous process is finished after the main process that is why the state.arrayActual never gets the value.

  return(
        <div>
        <div id="carousel-main">
        <div style={{ margin:20}}>
          <React_Bootstrap_Carousel
            animation={true}
            onSelect={this.onSelect}
            className="carousel-fade"
          >

          {this.state.arrayActual.map(listaImgs=>{
            return (<CaroucelArray url={listaImgs}/>);})
          }
          </React_Bootstrap_Carousel>
        </div>
        </div>
        <h1>Tu slide actual</h1>
        </div>
      )
Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
  • 1
    https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – epascarello Jun 02 '17 at 18:17
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Austin Greco Jun 02 '17 at 19:35

0 Answers0