1

When I create a simple app, I found that next.js will automatically do the server side render.

But when I tried to fetch the data from backend, I found that server side won't get the data.

How to fetch the data from server side? So that I can do the server side render?

components/test.js

import React, { Component } from 'react';
class Test extends Component {
    constructor(){
        super();
        this.state={
            'test':''
        }
    }

    setTest(){

        axios.get(serverName+'/api/articles/GET/test').then(response=>{

            let test;
            test = response.data.test;
            this.setState({test});

        }).catch(function (error) {
            console.log(error);
        });
    }
    }

    render() {
        return (
            <div>
                {this.state.test}
            </div>
        );
    }
}
export default Test;

backend is just like following:

function getTest(Request $request){
    return response()->json(['test'=>'this is a test']);
}

2 Answers 2

1

Next.js uses getInitialProps that is executed on the server on initial load only.

From docs

For the initial page load, getInitialProps will execute on the server only. getInitialProps will only be executed on the client when navigating to a different route via the Link component or using the routing APIs.

All other lifecycle methods/actions on React components (componentDidMount, onClick, onChange etc) are executed on the client side.

Example code

class Test extends Component {
  static async getInitialProps() {
    const response = await axios.get(serverName + '/api/articles/GET/test');

    return { test: response.data.test }
  }

  render() {
    return <div>{this.props.test}</div>;
  }
}

export default Test;
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I'm checking the document right now. Thanks for your advice.
i've updated the answer with the code. Hope this helps
0

Like below. I would recomend to use getInitialProps. This is recommended approach by next.js to get data at server.

import React from 'react'

export default class extends React.Component {
  static async getInitialProps({ req }) {
  
  axios.get(serverName+'/api/articles/GET/test').then(response=>{
            let test;
            test = response.data.test;
            return { test }
        }).catch(function (error) {
             return { response }
        });  
  }

  render() {
    return (
      <div>
        Hello World {this.props.test.name}
      </div>
    )
  }
}

1 Comment

Hello, I tried to run the code but failed. I add console.log('test: ',test); inside axios function and found that response can be receive at getInitialProps. But {this.props.test} can't get anything. Am I do something wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.