React fetch data in server before render

前端 未结 8 2021
自闭症患者
自闭症患者 2020-12-02 18:29

I\'m new to reactjs, I want to fetch data in server, so that it will send page with data to client.

It is OK when the function getDefaultProps return dummy data like

8条回答
  •  长情又很酷
    2020-12-02 18:57

    The best answer I use to receive data from server and display it

     constructor(props){
                super(props);
                this.state = {
                    items2 : [{}],
                    isLoading: true
                }
    
            }
    
    componentWillMount (){
     axios({
                method: 'get',
                responseType: 'json',
                url: '....',
    
            })
                .then(response => {
                    self.setState({
                        items2: response ,
                        isLoading: false
                    });
                    console.log("Asmaa Almadhoun *** : " + self.state.items2);
                })
                .catch(error => {
                    console.log("Error *** : " + error);
                });
        })}
    
    
    
        render() {
           return(
           { this.state.isLoading &&
                        
    
                    }
                    { !this.state.isLoading &&
                //external component passing Server data to its classes
                          
                    }
             ) }
    

提交回复
热议问题