react 학습내용입니다.
Today I Learned
- react 라이브러리
- react state
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
function tick(){
ReactDOM.render(
<section>
<h1>세계 시간</h1>
<section>
<h1>America/Toronto</h1>
<span>It is {new Date().toLocaleString("en-US",{timeZone:"America/Toronto"})}</span>
</section>
<section>
<h1>Asia/Seoul</h1>
<span>It is {new Date().toLocaleString("ko-KR",{timeZone:"Asia/Seoul"})}</span>
</section>
</section>
,document.querySelector('#root'));
}
setInterval(tick,1000);
function Clock(){
return <section>
<h1>America/Toronto</h1>
<span>It is {new Date().toLocaleString("en-US",{timeZone:"America/Toronto"})}</span>
</section>;
}
function tick(){
ReactDOM.render(
<section>
<h1>세계 시간</h1>
<Clock />
<Clock />
</section>
,document.querySelector('#root'));
}
setInterval(tick,1000);
function Clock(props){
let time = new Date().toLocaleString(props.locale,{timeZone:props.timeZone});
return <section>
<h1>{props.timeZone}</h1>
<span>It is {time}</span>
</section>;
}
function tick(){
ReactDOM.render(
<section>
<h1>세계 시간</h1>
<hr />
<Clock locale="en-US" timeZone="America/Toronto"/>
<hr />
<Clock locale="ko-KR" timeZone="Asia/Seoul"/>
</section>
,document.querySelector('#root'));
}
setInterval(tick,1000);
/*
class Clock extends React.Component{//객체지향적인 캡슐화 가능
#time;
constructor(props){
super(props);//필수 무조건 react.Component에 보내줘야함
this.#time = new Date().toLocaleString(props.locale,{timeZone:props.timeZone});
}
render(){
return <section>
<h1>{this.props.timeZone}</h1>
<span>It is {this.#time}</span>
</section>;
}
}*/
class Clock extends React.Component{//객체지향적인 캡슐화 가능
render(){
let time = new Date().toLocaleString(this.props.locale,{timeZone:this.props.timeZone});
return <section>
<h1>{this.props.timeZone}</h1>
<span>It is {time}</span>
</section>;
}
}
class Clock extends React.Component{//객체지향적인 캡슐화 가능
constructor(props){
super(props);//필수 무조건 react.Component에 보내줘야함
let time = new Date().toLocaleString(this.props.locale,{timeZone:this.props.timeZone});
let timeZone = props.timeZone;
this.state = {time,timeZone};
}
tick(){
let time = new Date().toLocaleString(this.props.locale,{timeZone:this.props.timeZone});
let timeZone = this.props.timeZone;
this.setState({time,timeZone});
}
componentDidMount(){//mount 된 후
/*
setInterval(function tick(){
let time = new Date().toLocaleString(this.props.locale,{timeZone:this.props.timeZone});
let timeZone = this.props.timeZone;
this.setState({time,timeZone});
}.bind(this),1000);
*/
setInterval(()=>{this.tick();},1000);
}
componentWillUnmount(){//mount 해제 전
console.log("will unmount");
}
render(){
return <section>
<h1>{this.state.timeZone}</h1>
<span>It is {this.state.time}</span>
</section>;
}
}
function tick(){
ReactDOM.render(
<section>
<h1>세계 시간</h1>
<hr />
<Clock locale="en-US" timeZone="America/Toronto"/>
<hr />
<Clock locale="ko-KR" timeZone="Asia/Seoul"/>
</section>
,document.querySelector('#root'));
}
tick();