简体   繁体   English

为什么addChangeListener应该在componentDidMount而不是componentWillMount?

[英]Why should addChangeListener be in componentDidMount instead of componentWillMount?

I saw this line as an answer to another question on here: 我在这里看到这一行作为另一个问题的答案:

"componentWillMount should be componentDidMount, or else you'll leak event emitters in node." “componentWillMount应该是componentDidMount,否则你将在节点中泄漏事件发射器。”

and I don't really understand it. 我真的不明白。 Can someone explain with more detail? 有人可以更详细地解释一下吗?

More info: 更多信息:

Building a react application with flux, as part of the initial render, a child component computes some data. 使用flux构建react应用程序,作为初始渲染的一部分,子组件计算一些数据。 Ideally, after this data is computed, I would like to call an action that updates the store's state with a portion of this new data. 理想情况下,在计算此数据之后,我想调用一个动作,用一部分新数据更新商店的状态。

Normally, updating the store's state emits a change event that causes a re-render. 通常,更新商店的状态会发出导致重新呈现的更改事件。 However, because the change listener isn't being added until componentDidMount (rather than in componentWillMount), my top level component isn't able to listen for the change that occurs during the initial render and initiate a re-render. 但是,因为更改侦听器直到componentDidMount(而不是componentWillMount)才被添加,所以我的顶级组件无法侦听初始渲染期间发生的更改并启动重新渲染。

If I move the addChangeListener to componentWillMount that would seem to fix this issue, but the above quote suggests that this is a bad idea? 如果我将addChangeListener移动到似乎可以修复此问题的componentWillMount,但上面的引用表明这是一个坏主意?

I think the prevailing wisdom that listeners should be set in componentDidMount because it prevents problems in isomorphic applications is a mistake. 我认为应该在componentDidMount设置监听器的普遍智慧,因为它可以防止同构应用程序中出现问题,这是一个错误。 I think in 98% of cases for non-isomorphic applications setting listeners in either componentWillMount and componentDidMount will work the same way, but it is conceptually wrong and in the 2% of cases (such as the example given in the original question) it will do the wrong thing. 我认为在非同构应用程序的98%的情况下,在componentWillMountcomponentDidMount设置侦听器将以相同的方式工作,但它在概念上是错误的,并且在2%的情况下(例如原始问题中给出的示例)它将做错了。

There are git issue discussions and comments in the React source code suggesting that it would be preferred that componentWillMount wasn't called on the server at all, but if it isn't then problems are created in the checksum test comparing the server prerender to the client initial render. 在React源代码中有git问题讨论和注释,建议最好不要在服务器上调用componentWillMount ,但如果不是,则在校验和测试中创建问题,比较服务器预呈现器和客户端初始渲染。 Having componentWillMount on the server means that it isn't executed as part of the component lifecycle in this case, but this is being used as an excuse to not count it as part of the lifecycle in any case. 在服务器上使用componentWillMount意味着在这种情况下它不会作为组件生命周期的一部分执行,但这被用作在任何情况下都不将其视为生命周期的一部分的借口。

In fact, componentWillMount is exactly the right place to register listeners if you are not creating an isomorphic application. 事实上,如果您没有创建同构应用程序, componentWillMount正是注册侦听器的正确位置。 If you are creating an isomorphic application then you have to make some compromises due to the checksum/lifecycle issue not being ideal in this case (maybe just testing for the server environment and then not registering listeners?). 如果您正在创建一个同构应用程序,那么由于校验和/生命周期问题在这种情况下不理想(可能只是测试服务器环境然后没有注册监听器?),您必须做出一些妥协。

In non-isomorphic applications adding listeners in componentWillMount can save unnecessary re-renders in some cases and will register them in document order. 在非同构应用程序中,在componentWillMount添加侦听器可以在某些情况下保存不必要的重新呈现,并将按文档顺序注册它们。 The advantage of document order is that if you have a way to flush pending events as components are re-rendered (for example, takeRecords on a MutationObserver ) then you can ensure the document is re-rendered top down instead of bottom up, converting the rendering complexity to linear from polynomial. 文档顺序的优点是,如果你有办法刷新挂起的事件作为组件重新渲染(例如, takeRecordsMutationObserver ),则可以保证文档重新呈现自上而下,而不是自下而上的,转换将复杂度从多项式渲染为线性。

Additionally, there is no danger period between the initial render and when the listener is registered where the Store can change without triggering a render, causing the view to be out of sync with the Store (the example problem given in the original question). 此外,在初始渲染和注册侦听器之间没有危险期间,Store可以在不触发渲染的情况下进行更改,从而导致视图与Store不同步(原始问题中给出的示例问题)。 If the listener is registered in componentDidMount you either need to make sure the Store isn't changed in componentDidMount calls in children, or else force a re-render/re-sync after registering the listener, which if done in componentDidMount is done in reverse document order which may be polynomial complexity (depending on how/if the React setStates are aggregated). 如果监听器在componentDidMount注册,则需要确保在子项中的componentDidMount调用中未更改Store,否则在注册监听器后强制重新呈现/重新同步,如果在componentDidMount中完成则反向完成文档顺序可能是多项式复杂性(取决于如何/如果聚合React setStates )。

Is hard to understand what that quote means without more context. 没有更多的背景,很难理解这句话的含义。 What I can tell you is that there are huge differences between the two of those methods. 我可以告诉你的是,这两种方法之间存在巨大差异。

On one hand, componentWillMount is called before the component is actually added to the DOM. 一方面, componentWillMount前组件实际添加到DOM被调用。 This is the last chance you have to update component's state and get it rendered before the component is rendered by the browser. 这是您必须更新组件状态并在浏览器呈现组件之前呈现它的最后一次机会。

On the other hand, componentDidMount is called once the component has been attached to the DOM (the real one). 在另一方面, componentDidMount一旦组件已被附加到DOM(真正的一个)被调用。

What you need really depends on your use case. 你需要什么取决于你的用例。 In general, componentDidMount is used to integrate with other libraries (like jQuery), it provides a way to modify the HTML rendered by the component. 通常, componentDidMount用于与其他库(如jQuery)集成,它提供了一种修改组件呈现的HTML的方法。

I suggest you to read these links: 我建议你阅读这些链接:

暂无
暂无

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

相关问题 何时使用componentWillMount而不是componentDidMount - when to use componentWillMount instead of componentDidMount 为什么不应该使用componentWillMount? - Why componentWillMount should not be used? 为什么异步请求应该在componentDidMount中而不是在ReactJS中的getInitialState中进行? - Why async requests should be made in componentDidMount instead of getInitialState in ReactJS? 在 React.js 中,我应该在 componentWillMount 还是 componentDidMount 中进行初始网络请求? - In React.js should I make my initial network request in componentWillMount or componentDidMount? 我需要使用componentDidMount或componentWillMount - componentDidMount or componentWillMount which one I need to use componentDidMount 或 componentWillMount 在 React-Native 中不起作用? - componentDidMount or componentWillMount not working in React-Native? 最好在 componentDidMount 或 componentWillMount 上执行 API 调用吗? - Is it best to perform an API call on componentDidMount or componentWillMount? 我应该如何替换componentWillMount()? - How should I alternate componentWillMount()? React Native组件未使用componentWillMount或componentDidMount中的异步redux操作呈现 - React Native component not rendering with async redux action in componentWillMount or componentDidMount 在React中使用componentWillMount或componentDidMount生命周期函数进行异步请求 - Use componentWillMount or componentDidMount lifecycle functions for async request in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM