Learning React(5.Reactイベント)
95348 ワード
1.活動
01.開始前準備
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
var destination = document.querySelector("#container");
</script>
</body>
</html>
02.UI実装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
class Counter extends React.Component {
render() {
var textStyle = {
fontSize : 78,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold'
};
return(
<div style={textStyle}>
{this.props.display}
</div>
)
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 0
};
}
render() {
var backgroundStyle = {
padding : 50,
backgroundColor : '#FFC53A',
width : 250,
height : 100,
borderRadius : 10,
textAlign : 'center'
};
var buttonStyle = {
fontSize : '1em',
width : 30,
height : 30,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold',
lineHeight : '3px'
};
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<button style={buttonStyle}>+</button>
</div>
);
}
}
ReactDOM.render(
<div>
<CounterParent/>
</div>,
document.querySelector("#container")
)
</script>
</body>
</html>
03.キー機能実現
登録
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
var destination = document.querySelector("#container");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
class Counter extends React.Component {
render() {
var textStyle = {
fontSize : 78,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold'
};
return(
<div style={textStyle}>
{this.props.display}
</div>
)
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 0
};
}
render() {
var backgroundStyle = {
padding : 50,
backgroundColor : '#FFC53A',
width : 250,
height : 100,
borderRadius : 10,
textAlign : 'center'
};
var buttonStyle = {
fontSize : '1em',
width : 30,
height : 30,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold',
lineHeight : '3px'
};
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<button style={buttonStyle}>+</button>
</div>
);
}
}
ReactDOM.render(
<div>
<CounterParent/>
</div>,
document.querySelector("#container")
)
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
class Counter extends React.Component {
render() {
var textStyle = {
fontSize : 78,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold'
};
return(
<div style={textStyle}>
{this.props.display}
</div>
)
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 0
};
this.increas = this.increas.bind(this);
}
increas(e) {
this.setState({
count : this.state.count + 1
})
}
render() {
var backgroundStyle = {
padding : 50,
backgroundColor : '#FFC53A',
width : 250,
height : 100,
borderRadius : 10,
textAlign : 'center'
};
var buttonStyle = {
fontSize : '1em',
width : 30,
height : 30,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold',
lineHeight : '3px'
};
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<button onClick={this.increas} style={buttonStyle}>+</button>
</div>
);
}
}
ReactDOM.render(
<div>
<CounterParent/>
</div>,
document.querySelector("#container")
)
</script>
</body>
</html>
04.合成イベント
https://ko.reactjs.org/docs/events.html
-1.システムイベントのプロパティ
-2.マウスシステムイベントのプロパティ
-3.キーボード・システムのイベント・プロパティ
05.アクティブ属性の利用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
class Counter extends React.Component {
render() {
var textStyle = {
fontSize : 78,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold'
};
return(
<div style={textStyle}>
{this.props.display}
</div>
)
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 0
};
this.increas = this.increas.bind(this);
}
increas(e) {
var currentCount = this.state.count;
currentCount = (e.shiftKey)? currentCount+=10 : currentCount+=1;
this.setState({
count : currentCount
})
}
render() {
var backgroundStyle = {
padding : 50,
backgroundColor : '#FFC53A',
width : 250,
height : 100,
borderRadius : 10,
textAlign : 'center'
};
var buttonStyle = {
fontSize : '1em',
width : 30,
height : 30,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold',
lineHeight : '3px'
};
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<button onClick={this.increas} style={buttonStyle}>+</button>
</div>
);
}
}
ReactDOM.render(
<div>
<CounterParent/>
</div>,
document.querySelector("#container")
)
</script>
</body>
</html>
06.その他のイベント処理方法
-1.構成部品のイベントを直接更新することはできません。
1.イベントを構成部品に直接渡す
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
class Counter extends React.Component {
render() {
var textStyle = {
fontSize : 78,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold'
};
return(
<div style={textStyle}>
{this.props.display}
</div>
)
}
}
class PlusButton extends React.Component {
render () {
var buttonStyle = {
fontSize : '1em',
width : 30,
height : 30,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold',
lineHeight : '3px'
};
return(
<button style={buttonStyle}> + </button>
)
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 0
};
this.increas = this.increas.bind(this);
}
increas(e) {
var currentCount = this.state.count;
currentCount = (e.shiftKey)? currentCount+=10 : currentCount+=1;
this.setState({
count : currentCount
})
}
render() {
var backgroundStyle = {
padding : 50,
backgroundColor : '#FFC53A',
width : 250,
height : 100,
borderRadius : 10,
textAlign : 'center'
};
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<PlusButton onClick={this.increas}/>
</div>
);
}
}
ReactDOM.render(
<div>
<CounterParent/>
</div>,
document.querySelector("#container")
)
</script>
</body>
</html>
2.イベント属性を構成部品のDOM要素に渡す
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React!</title>
<!-- 리액트 라이브러리와 리액트가 DOM에 대한 작업할 때 필요한 다양한 기능을 추가 -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- 바벨, 자바스크립트 컴파일러의 참조 -->
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #EEE;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
class Counter extends React.Component {
render() {
var textStyle = {
fontSize : 78,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold'
};
return(
<div style={textStyle}>
{this.props.display}
</div>
)
}
}
class PlusButton extends React.Component {
render () {
var buttonStyle = {
fontSize : '1em',
width : 30,
height : 30,
fontFmail : 'sans-serif',
color : '#333',
fontWeight : 'bold',
lineHeight : '3px'
};
return(
<button onClick={this.props.clickHandler} style={buttonStyle}> + </button>
)
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 0
};
this.increas = this.increas.bind(this);
}
increas(e) {
var currentCount = this.state.count;
currentCount = (e.shiftKey)? currentCount+=10 : currentCount+=1;
this.setState({
count : currentCount
})
}
render() {
var backgroundStyle = {
padding : 50,
backgroundColor : '#FFC53A',
width : 250,
height : 100,
borderRadius : 10,
textAlign : 'center'
};
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<PlusButton clickHandler={this.increas}/>
</div>
);
}
}
ReactDOM.render(
<div>
<CounterParent/>
</div>,
document.querySelector("#container")
)
</script>
</body>
</html>
07.イベントハンドラ内部のthis
function doSomething(e) {
console.log(this); // button element
}
var btn = document.querySelector("button");
btn.addEventListener('click', doSomething, false);
this.increas = this.increas.bind(this);
08.反応中のイベント処理...一体どうして。
-1.ブラウザの互換性
-2.パフォーマンスの向上
複雑なUIを持つアプリケーションでは、アクティブなハンドルを作成するほど、アプリケーションがより多くのメモリを占有します.
Reference
この問題について(Learning React(5.Reactイベント)), 我々は、より多くの情報をここで見つけました https://velog.io/@ansalstmd/Learning-React5.-React-이벤트-다루기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol