我该如何将此代码翻译成App.js代码



我想试着把这个index.js翻译成App.js,但我不知道我试过一次,但它没有显示任何文本

我应该在这里更改哪些代码,使其在App.js中工作,并减少index.js 中填充的代码

import React from 'react';
import ReactDOM from 'react-dom/client';
const root = ReactDOM.createRoot(document.getElementById('root'));
function Anotherwan(props){
return (
<div>
<h1> Time: {props.time.toLocaleTimeString()}</h1>
</div>
);
}
class Clock extends React.Component{
constructor(props){
super(props);
this.state = {time: new Date()};
}
componentDidMount(){
this.TimerID = setInterval(
() => this.tick(), 1000
);
}
componentWillUnmount(){
clearInterval(this.TimerID);
}
tick (){
this.setState({
time: new Date()
})
}
render (){
return (
<Anotherwan time={this.time.date} />
)
}
}
root.render(<Clock />);

从技术上讲,index.js是唯一一个将内容打印到屏幕上的文件,其他所有内容都打印到export/import上。如果您正在使用create-react应用程序,您将看到index.js导入<App />组件,然后对其进行渲染

您的App.js除了构建和导出<App />组件之外,没有执行任何其他操作。

如果你是React的新手,他们在网站上有教程和文档,你可以从他们那里找到更多信息!

最新更新