【发布时间】:2021-01-10 13:16:51
【问题描述】:
我正在尝试将父函数传递给反应中的子组件并在子组件中设置间隔以调用父函数..我知道这看起来很容易,但我是反应新手。 第一次间隔执行后,我收到此错误“this.func 不是函数” 这是我的代码..
export default class Child extends React.Component{
func;
constructor(props) {
super();
this.func = props.func;
}
componentDidMount() {
setInterval(() => {this.func() }, 1000);
}
componentWillUnmount() {
}
render() {
return (
<div>
<p>{this.props.value}</p>
</div>
);
}
}
import React from 'react';
import Child from './child';
export default class Parent extends React.Component {
constructor() {
super();
this.state = {
counter:0
}
}
increase() {
this.setState({counter:this.state.counter+1});
}
render() {
return (
<div>
<Child proc={this.increase} value={this.state.counter}></Child>
</div>
);
}
}
【问题讨论】:
标签: javascript reactjs components state intervals