πŸ“˜
Lif31up's Blog
  • Welcome! I'm Myeonghwan
  • How to Read the Pages
  • Fundamental Machine Learning
    • Foundational Work of ML: Linear/Logistic Regression
    • Early-stage of AI: Perceptron and ADALINE
    • What is Deep Learning?: Artificial Neural Network to Deep Neural Network
    • * Challenges in Training Nerual Network
  • Meta Learning
    • Overview on Meta Learning
    • Prototypical Networks for Few-shot Learning
    • Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
  • Natural Language Process
    • Bag-of-Words
  • Actionale AI
    • * Overview on Actionable AI
  • Front-end Development
    • Overview on Front-end Development
    • Learning React Basic
      • React Component: How They are Rendered and Behave in Browser
      • State and Context: A Key Function to Operate the React Application
      • Design Pattern for Higher React Programming
  • Songwriting
    • A Comprehensive Guide to Creating Memorable Melodies through Motif and Phrasing
  • Sound Engineering
    • How to Install and Load Virtual Studio Instruments
    • A Guide to Audio Signal Chains and Gain Staging
    • Equalizer and Audible Frequency: How to Adjust Tone of the Signal
    • Dynamic Range: the Right Way to Compress your Sample
    • Acoustic Space Perception and Digital Reverberation: A Comprehensive Analysis of Sound Field Simulat
  • Musical Artistry
    • What is Artistry: in Perspective of Modern Pop/Indie Artists
    • Visualizing as Musical Context: Choose Your Aesthetic
    • Analysis on Modern Personal Myth and How to Create Your Own
    • Instagram Management: Business Approach to Your Social Account
  • Art Historiography
    • Importance of Art Historiography: Ugly and Beauty Across Time and Space
    • Post-internet Art, New Aesthetic and Post-Digital Art
    • Brutalism and Brutalist Architecture
Powered by GitBook
On this page
  1. Front-end Development
  2. Learning React Basic

React Component: How They are Rendered and Behave in Browser

λ¦¬μ•‘νŠΈμ—μ„œ μ»΄ν¬λ„ŒνŠΈλŠ” ν•¨μˆ˜μ™€ 클래슀의 ν˜•νƒœλ‘œ μ •μ˜λ  수 μžˆμŠ΅λ‹ˆλ‹€.

  • ν•¨μˆ˜ν˜• μš”μ†Œ(component as function)λŠ” props 객체λ₯Ό 인자둜 λ°›μ•„ λ¦¬μ•‘νŠΈ μ›μ†Œλ₯Ό λ°˜ν™˜ν•˜λŠ” λ°©μ‹μœΌλ‘œ, κ°„κ²°ν•œ 선언이 νŠΉμ§•μž…λ‹ˆλ‹€.

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ ES6 클래슀 λ¬Έλ²•μœΌλ‘œ κ³„κΈ‰ν˜• ν•¨μˆ˜(component as class)λ₯Ό μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄μ œλŠ” 잘 μ‚¬μš©λ˜μ§€ μ•Šμ§€λ§Œ λͺ‡ κ°€μ§€ μΆ”κ°€ κΈ°λŠ₯듀을 ν™œμš©ν•  수 μžˆλ‹€λŠ” μž₯점이 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜/κ³„κΈ‰ν˜• μ»΄ν¬λ„ŒνŠΈλ₯Ό μ„ μ–Έν•˜λŠ” μ½”λ“œ
// decalre using function syntax
function Foo(props){
	return (
		<h1>Hello, {props.name}</h1>;	
	)
}
// declare using class syntax
class Food extends React.Component{
	render(){
		return <h1>Hello, {this.props.name}</h1>
	}
}

ν•¨μˆ˜ μš”μ†Œλ₯Ό 클래슀 μš”μ†Œλ‘œ λ³€ν™˜ν•˜κΈ°

ν•„μš”μ— λ”°λΌμ„œ ν•¨μˆ˜ μ»΄ν¬λ„ŒνŠΈλ₯Ό 클래슀둜 λ³€ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  1. React.Componentλ₯Ό ν™•μž₯ν•˜λŠ” λ™μΌν•œ μ΄λ¦„μ˜ ES6 클래슀λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

  2. render() λ©”μ„œλ“œλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€:

    function Foo(){
    	const [data, setData] = useState(null);
    }
  3. ν•¨μˆ˜μ˜ λ‚΄μš©μ„ render() λ©”μ„œλ“œ μ•ˆμœΌλ‘œ μ΄λ™ν•©λ‹ˆλ‹€.

  4. render() λ©”μ„œλ“œ λ‚΄λΆ€μ˜ propsλ₯Ό this.props둜 λ³€κ²½ν•©λ‹ˆλ‹€.

  5. 더 이상 ν•„μš”ν•˜μ§€ μ•Šμ€ ν•¨μˆ˜ 선언을 μ œκ±°ν•©λ‹ˆλ‹€.

λ¦¬μ•‘νŠΈμ—μ„œ μ‚¬μš©μž μ •μ˜ μ»΄ν¬λ„ŒνŠΈμ˜ 이름은 항상 λŒ€λ¬Έμžλ‘œ μ‹œμž‘ν•΄μ•Όν•©λ‹ˆλ‹€. μ΄λŠ” λ¦¬μ•‘νŠΈκ°€ μ†Œλ¬Έμžλ‘œ μ‹œμž‘ν•˜λŠ” λͺ¨λ“  μ»΄ν¬λ„ŒνŠΈλ₯Ό DOM νƒœκ·Έλ‘œ μ²˜λ¦¬ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

React Rendering

λ¦¬μ•‘νŠΈ μ—”μ§„/λ¦¬μ•‘νŠΈ DOM(react DOM/react engine)은 μ‚¬μš©μžκ°€ μ •μ˜ν•œ μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‹€μ œ DOM μ›μ†Œλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. 이 과정을 λ¦¬μ•‘νŠΈ λ Œλ”λ§(react rendering)이라고 ν•©λ‹ˆλ‹€. λ‹€μŒμ€ λ¦¬μ•‘νŠΈ DOM이 μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‹€μ œ DOM μ›μ†Œλ‘œ λ³€ν™˜ν•˜λŠ” 과정에 λŒ€ν•œ μ„€λͺ…μž…λ‹ˆλ‹€:

  1. 초기 λ Œλ”λ§(initial rendering)을 μ‹œμž‘ν•œλ‹€.

    1. ReactDOM.render() λ˜λŠ” createRoot().render()λ₯Ό 톡해 λ Œλ”λ§ ν”„λ‘œμ„ΈμŠ€λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.

    2. 가상 DOM(virtual DOM) 즉 λ¦¬μ•‘νŠΈ DOM을 κ΅¬λ™μ‹œν‚€κ³ , μ΄λŠ” μ£Όμ–΄μ§„ μ½”λ“œλ₯Ό 읽어 μ»΄ν¬λ„ŒνŠΈ κ°„μ˜ 계측 ꡬ쑰λ₯Ό ν‘œν˜„ν•œ μΌμ’…μ˜ μ§€λ„μž…λ‹ˆλ‹€.

    3. 이후, μ‹€μ œ DOMκ³Ό λΉ„κ΅ν•˜μ—¬ 차이점을 ν™•μΈν•©λ‹ˆλ‹€. λ§Œμ•½ 가상 DOMκ³Ό μ‹€μ œ DOM 사이에 차이가 μžˆλ‹€λ©΄, μ‹€μ œ DOM을 κ°±μ‹ ν•˜μ—¬ 두 DOM을 동기화해야 ν•©λ‹ˆλ‹€.

  2. 리-λ Œλ”λ§(re-rendering)을 ν˜ΈμΆœν•œλ‹€.

    1. useState, useReducer 등을 톡해 μƒνƒœκ°€ λ³€κ²½λ˜μ—ˆλ‹€λ©΄ ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈ λ˜λŠ” κ·Έ μžμ‹μ„ λͺ¨λ‘ λ‹€μ‹œ λžœλ”λ§ν•΄μ•Ό ν•©λ‹ˆλ‹€.

λžœλ”λ§μ„ ν˜ΈμΆœν•˜λŠ” μ½”λ“œ
// Calling Renderer as Functional Component
function Greeting({name}) {
	return <h1>Hello, {name}</h1>;
}
// Calling Renderer as Class Component
class Greeting extends React.Component {
	render() {
		return <h1>Hello, {this.props.name}</h1>;
	}
}

Event Handling/Event Handler

λ¦¬μ•‘νŠΈλŠ” JSX에 μ‚¬μš©μž μ •μ˜ 이벀트 ν•Έλ“€λŸ¬λ₯Ό μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” 클릭, ν˜Έλ²„, 포컀슀, μž…λ ₯κ³Ό 같은 μ‚¬μš©μž λ™μž‘μ— λ°˜μ‘ν•˜μ—¬ μ‹€ν–‰λ©λ‹ˆλ‹€. 이벀트 ν•Έλ“€λŸ¬λ₯Ό μΆ”κ°€ν•˜λ €λ©΄ λ¨Όμ € ν•¨μˆ˜λ₯Ό μ •μ˜ν•œ ν›„, 이λ₯Ό JSX νƒœκ·Έμ˜ μ†μ„±μœΌλ‘œ 전달해야 ν•©λ‹ˆλ‹€:

  1. μ»΄ν¬λ„ŒνŠΈ λ‚΄λΆ€ λ˜λŠ” 외뢀에 handleClick ν•¨μˆ˜λ₯Ό μ„ μ–Έν•˜μ—¬ μ›ν•˜λŠ” λ™μž‘μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€.

  2. <button> νƒœκ·Έμ— onClick={handleClick} 속성 μ •μ˜λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€:

    // Wrtie it in pre-rendering area (Nested Function Pattern)
    export default function Button(){
    	function handleClick(){
    		alert("You clicked me!");
    	}
    	return(
    		<button onClick={handleClick}>
    			Click Me
    		</button>
    	);
    }
    
    // Write it in HTML element (Inline Pattern)
    function Button__(){
    	return (<button onClick={function handleClick()=>{alert("you clicked me!");}})></button>)
    }

νŠΉμ„±(property)은 λ¦¬μ•‘νŠΈμ—μ„œ prop이라고 ν•©λ‹ˆλ‹€. ν•¨μˆ˜ μ»΄ν¬λ„ŒνŠΈμ™€ 클래슀 μ»΄ν¬λ„ŒνŠΈλŠ” μžμ‹ μ˜ propsλ₯Ό μˆ˜μ •ν•  수 μ—†κ³ , μˆ˜μ •ν•΄μ„œλ„ μ•ˆ λ©λ‹ˆλ‹€. ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜μ–΄λ„ μž…λ ₯값이 λ³€ν•˜μ§€ μ•ŠλŠ” 것을 순수 ν•¨μˆ˜λΌ ν•˜λŠ”λ°, propsλ₯Ό λ‹€λ£¨λŠ” λͺ¨λ“  ν•¨μˆ˜λŠ” 이런 순수 ν•¨μˆ˜μ²˜λŸΌ λ™μž‘ν•΄μ•Ό ν•©λ‹ˆλ‹€.

이벀트 ν—¨λ“€λŸ¬λ₯Ό μ •μ˜ν•˜λŠ” μ»¨λ²€μ…˜

이벀트 ν•Έλ“€λŸ¬λŠ” 일반적으둜 μ»΄ν¬λ„ŒνŠΈ λ‚΄λΆ€μ—μ„œ μ •μ˜λ©λ‹ˆλ‹€.

  • handle둜 μ‹œμž‘ν•˜κ³  이벀트 μ΄λ¦„μœΌλ‘œ λλ‚˜λŠ” λͺ…λͺ… κ·œμΉ™μ„ λ”°λ₯΄λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

    • μ˜ˆμ‹œ: onClick={handleClick}, onMouseEnter={handleMouseEnter}

이벀트 ν•Έλ“€λŸ¬λ₯Ό 톡해 νŠΉμ„±μ„ 읽기

이벀트 ν•Έλ“€λŸ¬λŠ” μ»΄ν¬λ„ŒνŠΈ λ‚΄λΆ€μ—μ„œ μ •μ˜λ˜λ―€λ‘œ ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈμ˜ props에 μžμ—°μŠ€λŸ½κ²Œ μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜ μ˜ˆμ‹œλ₯Ό 톡해 이벀트 ν•Έλ“€λŸ¬μ—μ„œ propsλ₯Ό μ½λŠ” 방법을 μ‚΄νŽ΄λ΄…λ‹ˆλ‹€.

function AlertButton({message,children}){
	return (
		<button onClick={()=>(alert(message))}>
			{children}
		</button>
	);//nest
}
export default function Toolbar(){
	return(
		<div>
			<AlertButton message="Playing!">
				Play Movie
			</AlertButton>
			<AlertButton message="Uploading!">
				Upload Image
			</AlertButton>
		</div>
	);//nest
}
이벀트 ν•Έλ“€λŸ¬λ₯Ό 톡해 νŠΉμ„±μ„ μ „νŒŒν•˜κΈ°

λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μžμ‹ μ»΄ν¬λ„ŒνŠΈμ˜ 이벀트 ν•Έλ“€λŸ¬λ₯Ό μ œμ–΄ν•˜λ €λ©΄ propsλ₯Ό 톡해 μ „λ‹¬ν•˜λ©΄ λ©λ‹ˆλ‹€. 도ꡬ창 μš”μ†ŒλŠ”<PlayButton>κ³Ό<UploadButton>을 λ Œλ”λ§ν•˜λŠ”λ°,handlePlayClick()을onClick()νŠΉμ„±μœΌλ‘œ <Button>에 μ „λ‹¬ν•©λ‹ˆλ‹€. ν›„μžμ˜ 경우 인라인 ν•¨μˆ˜λ₯ΌonClick()νŠΉμ„±μ„ 톡해 <Button>에 μ „λ‹¬ν•©λ‹ˆλ‹€.

function Button({onClick,children}){
  return(
      <button onClick={onClick}> {children} </button>
  );//nest
}
function PlayButton({movieName}){
  function handlePlayClick(){
    alert("Playing ${moiveName}");
  }
  return(
      <Button onClick={handlePlayClick}>Play {movieName} </Button>
  );//nest
}
function UploadBUtton(){
  return (
      <Button onClick={()=>alert("uploading")}> upload image </Button>
  );//nest
}
function UploadBUtton(){
  return (
      <PlayButton movieName="Kiki's Delivery Service"/>
      <UploadButton/>
  );//nest
}
PreviousLearning React BasicNextState and Context: A Key Function to Operate the React Application

Last updated 4 days ago