为什么在componentDidMount()期间必须解析函数中的函数调用?

时间:2019-06-23 20:12:54

标签: javascript reactjs function components document

我正在查看react Doc上的状态和生命周期页面。为什么必须在已解析的函数中调用函数?

我尝试删除已解析的函数,仅调用this.tick(),但没有用。

class Clock extends React.Component{
    constructor(props){
        super(props);
        this.state = {date: new Date()};
    }

    componentDidMount(){
        this.timerID = setInterval(()=>this.tick(), 1000)
    }

    componentWillUnmount(){
        clearInterval(this.timerID)
    }

    tick(){
        this.setState({
            date: new Date()
        })
    }

    render(){
        return (
                <h1>{new Date().toLocaleTimeString()} </h1>
        );
    }
}

1 个答案:

答案 0 :(得分:0)

因为您需要将引用传递给要执行的功能,所以如果传递this.tick()是对要执行的功能的调用,则可以传递给this.tick并且也应该起作用< / p>