- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我理解正确,组件的 React 生命周期应该确保在 componentWillReceiveProps
之前调用 componentDidMount
。当我在组件的初始安装上测试它时,它似乎是这样工作的。但是,当该组件之前已安装并重新安装时,顺序则相反。这是预期的行为吗?以下代码段说明了可能通过这种方式引入的潜在错误:
class Example extends React.Component {
componentDidMount() {
this.something = { foo: 'bar' };
}
componentWillReceiveProps(nextProps) {
this.something.foo;
// Throws a TypeError if this code is reached before
// componentDidMount is called.
}
}
最佳答案
简短回答:
确实不能保证这些方法的触发顺序。这就是为什么(如您的示例中)在 props 和 state 之外使用组件变量不是一个好主意的原因之一。
更好:如果您需要在生命周期事件之外使用它,请将您的 { foo: 'bar' }
置于状态中。
较长的答案:
componentDidMount()
每个生命周期仅调用一次:
componentDidMount
之后)componentWillReceiveProps()
不会在生命周期中的第一次渲染时调用,但会在所有后续渲染中调用 = 当父级再次发送 props 时。
通常情况下,第二次渲染(触发)componentWillReceiveProps
将在组件(及其子组件)完成安装之后发生,因此是在componentDidMount()
之后。
但我可以想到至少 1 个(也许是理论上的)顺序会颠倒的场景:
componentWillReceiveProps()
已触发,(但 componentDidMount
尚未触发)componentDidMount()
将触发。因此 componentDidMount()
不是初始化组件变量的好地方,例如 { foo: 'bar' }
。componentWillMount()
将是一个更好的生命周期事件。
但是,我不鼓励在 React 组件中使用组件范围的变量,并坚持设计原则:
关于reactjs - 生命周期: componentWillReceiveProps called before componentDidMount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37410633/
我正在使用使用 ReactSwipableView 包的 Material ui SwipeableViews,我在控制台上收到此错误 react-dom.development.js:12466 W
在我的其他类中,componentWillReceiveProps 工作得很好,但由于某种原因,它在这里没有触发。 ItemView.jsx class ItemView extends React.
我对 React 开发还很陌生,但我已经使用 React+Redux 开发了 3 个大项目,并且我看到了一个我非常不喜欢的模式: componentWillReceiveProps(nextProps
我在构造函数中定义了一个状态,就像这样 this.state={ datainaccordion:'', }; 现在在我的 componentWil
我有以下组件代码: var Answer = React.createClass({ getInitialState: function(){ return { comments:
我在 React 中遇到了一个大问题 - 可能是缺乏理解。我有一个进行回调的子组件,并具有一个 componentWillReceiveProps 函数。问题是我无法在 child 持有的文本输入上书
我最近想升级我的知识React ,所以我从组件生命周期方法开始。让我好奇的第一件事是这个componentWillReceiveProps .所以,文档说当组件接收新的(不一定是更新的) Prop 时
我有以下高阶组件来在我的组件上创建一个负载: import React, {Component} from 'react'; import PropTypes from 'prop-types'; i
我正在尝试在收到新 Prop 后立即更新子组件。但是,我的子组件中的 componentWillReceiveProps() 在 Prop 实际更新之前被调用。看完this article我明白为什么
在我的子组件中使用以下方法更新 Prop 更改状态,效果很好 componentWillReceiveProps(nextProps) { // update original state
我的直觉告诉我不行,但我很难想到更好的方法。 目前,我有一个显示项目列表的组件。根据提供的 props,此列表可能会发生变化(即过滤更改或上下文更改) 例如,给定一个新的 this.props.typ
所以我是 react 新手,想知道如何创建一个传递一些参数的页面并根据这些参数获取文档。但是,当我尝试使用 componentWillReceiveProps 时,我发现它没有运行,我不知道为什么。那
我是 React 新手,正在经历 React 的生命周期,但是我对 componentWillReceiveProps 的使用有点困惑。 谁能解释一下在什么情况下应该使用它。 谢谢。 最佳答案 当 p
我有一个由react-router (path="profile/:username") 加载的Profile 组件,该组件本身如下所示: ... import { fetchUser } from
这个问题已经有答案了: Can getDerivedStateFromProps be used as an alternative to componentWillReceiveProps (1 个
我通读了https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#fetching-external-data-when-p
我是 React/Redux 新手,并且对状态有疑问。 TrajectContainer.jsx class TrajectContainer extends React.Component {
我有一个组件,它由父组件通过传递 prop 来更新。在 componentWillReceiveProps 中,我想更改一个状态(availableData),其中包含来自 prop(newData)
在我拥有的组件中,我使用 componentWillReceiveProps() 异步设置组件中的多个状态。父组件正在向该子组件发送新的 props。我的期望是,每当 child 获得新的 Prop
我有一个连接到 redux 存储的组件,并且有一个如下所示的代码块: if (this.props.person !== nextProps.person) { ... } else {
我是一名优秀的程序员,十分优秀!