想知道为什么我没有访问我应该访问的变量真的很难。反应原生传递的道具
我有一个构造
constructor(props) {
super(props);
this.state = {
endpoint:props.url
};
console.log(props.url);// Prints url as intended
console.log(this.state.endpoint);// Also prints as intended
}
现在,你会认为这只会延续到componentDidMount,但它不?我认为一个构造函数应该启动一些状态?
componentDidMount(){
fetch(this.state.endpoint).then(etc....)
// this fails. If I do console.log(this.state.endpoint) I get undefined.
}
如何让端点在获取函数中工作?
那么,工作。不知道为什么它应该是这样的......没有任何意义。但是,好吧。 – KyleK
在构造函数之外,您需要使用'this.props'来访问'props'。我怀疑这是问题。乐意效劳 –