简体   繁体   English

Next.js:ComponentWillMount 与 getInitialProps

[英]Next.js: ComponentWillMount vs. getInitialProps

I'm using Next.js for my React application because it has server side rendering.我将Next.js用于我的 React 应用程序,因为它具有服务器端渲染。 As I checked by log, both methods ComponentWillMount and getInitialProps both run on the server side.正如我通过日志检查的那样, ComponentWillMountgetInitialProps两种方法都在服务器端运行。 Are there any differences between those methods?这些方法之间有什么区别吗?

When should I run in ComponentWillMount and when should I run in getInitialProps ?什么时候应该在ComponentWillMount运行,什么时候应该在getInitialProps运行?

I don't see Next.js mentions about this thing.我没有看到 Next.js 提到这件事。

WARNING警告

50% of the accepted answer is wrong. 50% 的接受答案是错误的。 Here's why.这是为什么。 Let me split my answer in two sections:让我把我的答案分成两部分:

Part 1:第1部分:

  1. GetInitialProps is usually an async function which is good for asynchronous operations at the server and passes data to the page as props. GetInitialProps 通常是一个异步函数,它适用于服务器的异步操作,并将数据作为 props 传递到页面。

  2. In Next.js it always runs at the server, if the page is called using Link then it is only called at the client side在 Next.js 中,它总是在服务器端运行,如果页面是使用 Link 调用的,那么它只会在客户端调用

Wrong , GetInitialProps get executed on both the server and browser (remember the goal of Next.js is to make universal JavaScript).错误,GetInitialProps 在服务器浏览器被执行(记住 Next.js 的目标是制作通用的 JavaScript)。 Here is what the documentation says:以下是文档中的内容:

With that, we can fetch data for a given page via a remote data source and pass it as props to our page.有了这个,我们可以通过远程数据源获取给定页面的数据,并将其作为道具传递给我们的页面。 We can write our getInitialProps to work on both server and the client.我们可以编写我们的getInitialProps来在服务器和客户端上工作。 So, Next.js can use it on both client and server.因此,Next.js 可以在客户端和服务器上使用它。

Part 2:第2部分:

The second section is to answer the actual question more accurately.第二部分是更准确地回答实际问题。 It's clear that the OP got confused between ComponentDidMount and ComponentWillMount .很明显,OP 混淆了ComponentDidMountComponentWillMount Because in the Next.js case it makes more sense to compare GetInitialProps vs. ComponentDidMount as they both able to make Async call to fetch data, and they also both allow rendering afterwards (which is not possible in the case of ComponentWillMount ).因为在Next.js情况下,它更有意义的比较GetInitialProps与ComponentDidMount因为它们都能够进行异步调用来获取数据,而且他们也都允许渲染之后(这是不可能ComponentWillMount的情况下)。

In any case you use one or another, there are a few differences:在任何情况下,您使用一种或另一种,都存在一些差异:

GetInitialProps : is provided by Next.js and it is not always triggered, so be careful with that. GetInitialProps :由 Next.js 提供,它并不总是被触发,所以要小心。 It happens when you wrap one component inside another.当您将一个组件包装在另一个组件中时,就会发生这种情况。 If the parent component has GetInitialProps, the child's GetInitialProps will never be triggered.如果父组件有 GetInitialProps,则永远不会触发子组件的 GetInitialProps。 See this thread for more information.有关更多信息,请参阅此线程

ComponentDidMount : is React implementation and it's always triggered on the browser. ComponentDidMount :是 React 的实现,它总是在浏览器上触发。

You might ask: 'so when should I use this or that?'.您可能会问:“那么我什么时候应该使用这个或那个?”。 Actually it is very confusing and at the same time very simple.实际上,它非常令人困惑,同时又非常简单。 Here is a rule of thumb:这是一个经验法则:

  • Use GetInitialProps to fetch data when your component acts as a page , and you want to provide the data as Props.当您的组件充当页面并且您希望将数据作为 Props 提供时,使用 GetInitialProps 来获取数据。
  • Use ComponentDidMount on children components (that are not pages) or when you want to update the state upon an Ajax call.在子组件(不是页面)上使用 ComponentDidMount,或者当您想在 Ajax 调用时更新状态时使用。

GetInitialProps获取初始道具

  1. GetInitialProps is a usually an async function which is good for asynchronous operations at the server and passes data to the page as props. GetInitialProps 通常是一个异步函数,它适用于服务器的异步操作,并将数据作为 props 传递到页面。

  2. In Next.js it always runs at the server, if the page is called using Link then it is only called at the client side.在 Next.js 中,它总是在服务器端运行,如果页面是使用 Link 调用的,那么它只会在客户端调用。

  3. It can only be used in pages not in components.它只能在页面中使用,不能在组件中使用。

ComponentWillMount组件挂载

  1. It is a lifecyle hook.它是一个生命周期钩子。 It is called just before the render method is called.它在调用 render 方法之前被调用。 Data fetched in it cannot be passed in as a prop.在其中获取的数据不能作为 prop 传入。

  2. It can be called in a component as well as in pages.它可以在组件和页面中调用。 It is not a good place to make asynchronous calls as it doesn't wait for the async operation to complete.这不是进行异步调用的好地方,因为它不会等待异步操作完成。 So if it runs at the server and your async operation is written in it, it will not get completed and it comes to the client without getting data.因此,如果它在服务器上运行并且您的异步操作写入其中,它将无法完成,并且在没有获取数据的情况下到达客户端。

componentWillMount is the Component Lifecycle methods. componentWillMount组件生命周期方法。

According to documentation根据文档

componentWillMount() is invoked immediately before mounting occurs. componentWillMount()在挂载发生前立即被调用。 It is called before render() , therefore calling setState() synchronously in this method will not trigger an extra rendering.它在render()之前调用,因此在此方法中同步调用setState()不会触发额外的渲染。 Generally, we recommend using the constructor() instead.通常,我们建议改用constructor() Avoid introducing any side-effects or subscriptions in this method.避免在此方法中引入任何副作用或订阅。 For those use cases, use componentDidMount() instead.对于这些用例,请改用componentDidMount() This is the only lifecycle hook called on server rendering.这是在服务器渲染上调用的唯一生命周期钩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM