我是新手,想知道如何创建一个页面,可以传递几个参数并根据这些参数获取文档。但是,当我尝试使用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>
)
}
}
是因为我尝试在状态中还没有任何内容时就渲染状态吗?感觉好像是这样...至少我猜测我会得到一个空对象作为文档状态。
componentWillReceiveProps
方法的工作方式无关紧要吗? - Arnold Gandarillas