0

I have a map function to a state that I'm sure it is not empty because its states have been logged.

When i load the page it shows the div "did Load", but thats about it. Console also does not register he "running" log.

This is for CS50W course, so I am using in-browser Babel transformer. Someone suggested i completly switched my approach to compiling the code but that would mean learing and making a whole new setup, so I was hoping that is not the reason this is not working.

class Posts extends React.Component{
    
    
    constructor(props){     
        super(props)
        this.state = {
            aposts: [],
            isLoaded: false
        }
    } 

    componentDidMount() {
        console.log('running go fetch');
        let currentList = [];
        fetch('/posts')
        .then(response => response.json())
        .then(posts => {
        
            
            
            for (var i = 0; i < posts.length; i++) {
                
                
                currentList.push(posts[i])
                console.log(currentList[i])
                
                
               
                
            }
            
            
        });
        console.log(currentList)

        this.setState({ aposts: currentList, isLoaded: true }, () => {
        console.log(this.state.aposts, 'aposts');
        }); 
        
    } 
     
    
    render(){
        var isLoaded = this.state.isLoaded
        if (isLoaded = true){
        return (
            <div>
                <div>did load</div>
                {this.state.aposts.map(function(apost) {
                console.log("running")    
                return (<li key = {apost.id}> {apost.id} </li>)
                })}
                
            </div>
            );          
        }
        else{
            <div>No load</div>
        }
    }
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • 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) – Emile Bergeron Jan 07 '21 at 20:45

1 Answers1

0
 componentDidMount() {
        console.log('running go fetch');
        let currentList = [];
        fetch('/posts')
        .then(response => response.json())
        .then(posts => {
        
            for (var i = 0; i < posts.length; i++) {
                currentList.push(posts[i])
                console.log(currentList[i])
            }
            
            console.log(currentList)

            this.setState({ aposts: currentList, isLoaded: true }, () => {
             console.log(this.state.aposts, 'aposts');
            
            });
        });

When making api call it take some time so JS just run that work on some other process and your main process keep working.

Read about sync/asyn

and when you use .then it was when JS complete api call and get the response from server it should run anything which is inside .then

So you have to set state in .then so it will set value for state after it fetch values


You said you have to setup and learn to do setup. Why are you not using [CRA][1] it will setup everything for you.
Rajan Lagah
  • 2,373
  • 1
  • 24
  • 40
  • I thought that my
    did load
    loading, which only happens if isLoaded is true, which means that my state JS already finsihed loading/running and i would have no async problems. Is that not so? I am not sure what you mean with [CRA][1]
    – Lorenzo Giusti Jan 07 '21 at 19:47
  • Putting the thisState inside .then worked, Thanks a lot! – Lorenzo Giusti Jan 07 '21 at 19:54
  • @LorenzoGiusti Happy to know that answer work. Will you please Upvote and accept the answer ? – Rajan Lagah Jan 08 '21 at 05:23