为什么在Next.js中调用了两次组件?

为什么在Next.js中调用了两次组件?,next.js,Next.js,这是我的next.js项目的pages/index.js const Index = () => { console.log('Index Component Called'); return ( <div>Hello</div> ) } export default Index; const Index=()=>{ log('Index Component Called'); 返回( 你好 ) } 出口违约指数; 控制台日志函数在客户端调

这是我的
next.js
项目的
pages/index.js

const Index = () => {
  console.log('Index Component Called');
  return (
    <div>Hello</div>
  )
}

export default Index;
const Index=()=>{
log('Index Component Called');
返回(
你好
)
}
出口违约指数;

控制台日志函数在客户端调用两次,在next.js节点服务器上调用一次,我猜是因为
next.js
中的
页面是服务器端呈现的(或预呈现的)。
因此,在这种情况下,当next.js呈现您的页面(服务器端)时,脚本将
console.log(“调用的索引组件”)
然后在前端做出反应,这样来自服务器的所有代码都应该再次执行。
在next.js中,您可以在
getInitialProps
中执行服务器端代码,并且只能在页面中执行,不能在组件中执行