小问题-不确定如何在其父程序被卸载后阻止其更新。
目前,我有一个“视图”(父视图),其中嵌套了一个TextField组件。
我的TextField组件用onBlur实现了输入
<input type={this.props.type} name={this.props.name} onBlur={this.handleBlur} ... />
TextField组件中的两个函数是
hideClear: function () {
// to prevent clear button from disappearing when clicking on it
if (
在本代码沙箱中
我试图在子组件上调用setState on componentDidMount
这难道不是一个好做法吗?为什么,如果不是,我们应该在哪里执行setstate,以及何时可以在setState中使用componentDidMount
我在本文中看到,在fetchData in componentDidMount 中提到了
请建议
我刚看了一下关于setState() I componentDidMount()的事。
您可以看到,在render()函数之后,componentDidMount()函数将被React调用。当您将setState()调用放入componentDidMount()中时,将导致重新呈现整个组件树--不仅是当前组件--不要忘记,当前组件刚刚完成呈现。
还有一些人建议把setState()的电话放在componentWillMount()里面。在某些情况下,我希望获得呈现元素的高度,并将其存储为状态,而上述方法无法工作。我还查看了React官方网站,它建议在componentDidMount(
在以下代码中:
componentDidUpdate(){
let self = this;
this.state.carsOfChoosenDriver = []; // array where I want to save the cars belonging to the choosenDriver
if (typeof this.state.choosenDriver.cars !== 'undefined'){
this.state.choosenDriver.cars.forEach(function(entry) {