【发布时间】:2020-06-04 10:03:38
【问题描述】:
我写了一个函数。我只使用简单的字母作为该函数的名称。我没有得到任何输出和错误。我突出显示了这个功能。
第三课.js
import React from 'react'
function formattedDate(props){
return(
<div>This is {props.date.toLocaleTimeString()}</div>
);
}
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(
<div><h1><formattedDate date={this.state.date}/></h1></div>
);
}
}
export default Clock
所以我检查了我的整个代码。但是我没有发现任何错误。将函数名称更改为大写名称后,我得到了输出。请说明原因。
import React from 'react'
function FormattedDate(props){
return(
<div>This is {props.date.toLocaleTimeString()}</div>
);
}
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(
<div><h1><FormattedDate date={this.state.date}/></h1></div>
);
}
}
export default Clock
我将 Lesson3.js 导入到 App.js。除了我上面提到的错误之外,我的代码中没有错误。
App.js
import React from 'react';
import './App.css';
import Clock from './Components/lesson3'
function App() {
return (
<div className="App">
<p>
<Clock/>
</p>
</div>
);
}
export default App;
【问题讨论】:
标签: javascript