- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我是 react 新手,想知道如何创建一个传递一些参数的页面并根据这些参数获取文档。但是,当我尝试使用 componentWillReceiveProps
时,我发现它没有运行,我不知道为什么。那么有人可以用最简单的术语解释一下什么是 componentWillReceiveProps
、它何时运行及其用途吗?我花了很多时间尝试阅读 React 页面,但自从我最近才开始 React 以来,所有这些对我来说似乎都是一种全新的语言。您是否还可以编辑下面的代码,以便它可以工作,并且我可以亲眼看到它如何与其他东西一起工作(当我亲眼看到它时,它可以帮助我更好地理解)。
下面是我的页面的代码:
import React from "react";
import { Tracker } from "meteor/tracker";
import { Link } from "react-router-dom"
import Menu from "./Menu"
import { Notes } from "../methods/methods";
export default class fullSize extends React.Component{
constructor(props){
super(props);
this.state = {
doc: {}
};
}
componentwillMount() {
Meteor.subscribe("notes");
}
componentWillReceiveProps(nextProps) {
this.tracker = Tracker.autorun(() => {
const doc = Notes.findOne(nextProps.match.params.noteId);
this.setState({ doc })
})
}
renderNote(){
console.log(this.state.doc)
}
render(){
return (
<div>{this.renderNote()}</div>
)
}
}
是因为我试图在其中有任何内容之前渲染状态吗?感觉就像我......这至少是我的猜测,正如文档所述,我得到了一个空对象。
最佳答案
基本概念是我们有这些类型的生命周期方法:
1- 安装方法:(在该组件的生命周期中仅被调用一次)
2-更新方法:(每当组件中发生任何更新时都会调用)
3-卸载方法:(组件卸载时)
<小时/>componentWillReceiveProps
是一个更新方法,仅当 props 值发生任何变化时才会运行,它不会在初始渲染时运行,因此您需要同时使用 componentWillReceiveProps
和 componentDidMount 方法。 componentDidMount
将获取初始数据,如果该页面收到新的 props,则 componentWillReceiveProps
将获取新数据。
componentWillReceiveProps() is invoked before a mounted component receives new props. React doesn't call componentWillReceiveProps with initial props during mounting. It only calls this method if some of component's props may update.
componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering.
这样写:
export default class fullSize extends React.Component{
constructor(props){
super(props);
this.state = {
doc: {}
};
}
componentwillMount() {
Meteor.subscribe("notes");
}
componentDidMount() {
this.tracker = Tracker.autorun(() => {
const doc = Notes.findOne(this.props.match.params.noteId);
this.setState({ doc })
})
}
componentWillReceiveProps(nextProps) {
if(this.props.match.params.noteId != nextProps.match.params.noteId)
this.tracker = Tracker.autorun(() => {
const doc = Notes.findOne(nextProps.match.params.noteId);
this.setState({ doc })
})
}
renderNote(){
console.log(this.state.doc)
}
render(){
return (
<div>{this.renderNote()}</div>
)
}
}
关于reactjs - react - componentWillReceiveProps 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45901625/
我正在使用使用 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 {
我是一名优秀的程序员,十分优秀!