Reactjs 反应-组件将接收未运行的道具

Reactjs 反应-组件将接收未运行的道具,reactjs,meteor,Reactjs,Meteor,所以我是个新手,我想知道如何创建一个传递一些参数的页面,并根据这些参数获取文档。然而,当我尝试使用组件willreceiveprops时,我发现它没有运行,我不知道为什么。那么,有人能用最简单的术语解释一下什么是组件将在何时运行以及它的用途吗?我花了很多时间试图阅读react页面,但所有这些对我来说似乎都是一种全新的语言,因为我最近才开始使用react。你也可以编辑下面的代码,让它工作,我可以看到它如何与其他东西一起工作(这有助于我更好地理解,当我看到它为自己) 以下是我的页面代码: impor

所以我是个新手,我想知道如何创建一个传递一些参数的页面,并根据这些参数获取文档。然而,当我尝试使用
组件willreceiveprops
时,我发现它没有运行,我不知道为什么。那么,有人能用最简单的术语解释一下什么是
组件将在何时运行以及它的用途吗?我花了很多时间试图阅读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>
    )
  }
}
从“React”导入React;
从“流星/跟踪器”导入{Tracker};
从“反应路由器dom”导入{Link}
从“/”菜单导入菜单
从“./methods/methods”导入{Notes};
导出默认类fullSize扩展React.Component{
建造师(道具){
超级(道具);
此.state={
文件:{}
};
}
组件willmount(){
Meteor.认购(“票据”);
}
组件将接收道具(下一步){
this.tracker=tracker.autorun(()=>{
const doc=Notes.findOne(nextrops.match.params.noteId);
this.setState({doc})
})
}
renderNote(){
console.log(this.state.doc)
}
render(){
返回(
{this.renderNote()}
)
}
}

是因为我试图在状态出现之前呈现它吗?我觉得我是……这是我的猜测,至少我得到了一个空对象作为doc状态

基本概念是我们有以下类型的生命周期方法:

1-安装方法:(仅在该组件的生命周期内调用一次)

2-更新方法:(在组件中发生任何更新时都将被调用)

3-卸载方法:(组件将卸载时)


componentWillReceiveProps
是一种更新方法,仅当道具值发生任何更改时才会运行,它不会在初始渲染时运行,因此您需要同时使用
componentWillReceiveProps
componentDidMount
方法
componentDidMount
将获取初始数据,如果该页面接收到新的道具,则
componentWillReceiveProps
将获取新数据

:

在装入组件之前调用componentWillReceiveProps() 接收新道具。React不调用componentWillReceiveProps和 安装过程中的初始支柱。它仅在以下情况下调用此方法: 组件的道具可能会更新

:

componentDidMount()在创建组件后立即调用 安装。需要DOM节点的初始化应该在这里进行。如果你 需要从远程端点加载数据,这是一个好地方 实例化网络请求。此方法中的设置状态将 触发重新渲染

这样写:

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>
    )
  }
}
导出默认类fullSize.Component{
建造师(道具){
超级(道具);
此.state={
文件:{}
};
}
组件willmount(){
Meteor.认购(“票据”);
}
componentDidMount(){
this.tracker=tracker.autorun(()=>{
const doc=Notes.findOne(this.props.match.params.noteId);
this.setState({doc})
})
}
组件将接收道具(下一步){
if(this.props.match.params.noteId!=nextrops.match.params.noteId)
this.tracker=tracker.autorun(()=>{
const doc=Notes.findOne(nextrops.match.params.noteId);
this.setState({doc})
})
}
renderNote(){
console.log(this.state.doc)
}
render(){
返回(
{this.renderNote()}
)
}
}

您是否有任何错误?不管您的状态在开始时是否为空,您如何测试
组件是否会接收Props
方法是否有效?@EdgarHenriquez没有错误只是一个空白屏幕我没有问题,但我确实收到一条警告:“警告:设置状态(…):只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用了setState()。这是禁止操作。请检查完整大小组件的代码。“您能显示完整组件(已更新)吗?您在componentWillMount或componentWillUnmount方法中使用的是setState吗?”?