πŸ“˜
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

State and Context: A Key Function to Operate the React Application

μš”μ†ŒλŠ” μƒν˜Έμž‘μš©μ˜ κ²°κ³Όλ₯Ό 좜λ ₯ν•΄μ•Ό ν•  ν•„μš”λ₯Ό κ°€μ§‘λ‹ˆλ‹€. 이λ₯Ό μœ„ν•΄ 각 μš”μ†Œκ°€ λ…λ¦½μ μœΌλ‘œ μ €μž₯곡간을 κ°€μ Έμ•Ό ν•˜λŠ”λ° 이λ₯Ό μƒνƒœ(state)라고 ν•©λ‹ˆλ‹€.

μ§€μ—­λ³€μˆ˜(local variable)
μƒνƒœ(state)

μ§€μ—­λ³€μˆ˜λŠ” λ Œλ”λ§μ΄ μ’…λ£Œλœ ν›„μ—λŠ” μœ μ§€λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ Œλ”λ§ 호좜이 λλ‚˜λ©΄ λͺ¨λ“  μ§€μ—­λ³€μˆ˜κ°€ ν•΄μ œλ©λ‹ˆλ‹€.

μƒνƒœλŠ” λ Œλ”λ§μ΄ μ’…λ£Œλœ 후에도 값을 μœ μ§€ν•©λ‹ˆλ‹€.

μ§€μ—­λ³€μˆ˜μ˜ λ³€ν™”λŠ” μƒˆλ‘œμš΄ λ Œλ”λ§μ„ λ°œμƒμ‹œν‚€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

μƒνƒœμ˜ λ³€ν™”λŠ” μƒˆλ‘œμš΄ λ Œλ”λ§μ„ λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.

생λͺ… 주기와 λ§ˆμš΄νŒ…

λ¦¬μ•‘νŠΈμ—μ„œ μƒνƒœλŠ” propκ³Ό μœ μ‚¬ν•˜μ§€λ§Œ μ§€μ—­μ μœΌλ‘œ λ™μž‘ν•©λ‹ˆλ‹€. λͺ¨λ“  μƒνƒœλŠ” ν•΄λ‹Ή μš”μ†Œκ°€ μ™„μ „νžˆ μ œμ–΄ν•©λ‹ˆλ‹€. 이λ₯Ό μ œμ–΄ν•˜λŠ” 과정을 μ»΄ν¬λ„ŒνŠΈ 생λͺ…μ£ΌκΈ°(component lifecycle)라고 ν•©λ‹ˆλ‹€. μƒνƒœ 값이 변경될 λ•Œλ§ˆλ‹€ ν•΄λ‹Ή μš”μ†ŒλŠ” λ‹€μ‹œ λ Œλ”λ§λ˜λ©°, μ‚¬μš©μžλŠ” 이 νŠΉμ„±μ„ ν™œμš©ν•˜μ—¬ λ‹€μ–‘ν•œ λ™μž‘μ„ μ œμ–΄ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ§ˆμš΄νŒ…(mounting)은 μ»΄ν¬λ„ŒνŠΈκ°€ DOM에 μΆ”κ°€λ˜μ–΄ μž‘λ™ν•˜κΈ° μ‹œμž‘ν•  λ•Œμ˜ 생λͺ…μ£ΌκΈ° 단계λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€. λ§ˆμš΄νŒ…μ€ λ‹€μŒ μ„Έ λ‹¨κ³„λ‘œ μ§„ν–‰λ©λ‹ˆλ‹€.

  1. μ΄ˆκΈ°ν™” λ‹¨κ³„μ—μ„œλŠ” μ»΄ν¬λ„ŒνŠΈ ν΄λž˜μŠ€κ°€ μΈμŠ€ν„΄μŠ€ν™”λ©λ‹ˆλ‹€. μ΄λ•Œ μƒμ„±μžκ°€ ν˜ΈμΆœλ˜μ–΄ μƒνƒœμ™€ propsκ°€ μ΄ˆκΈ°ν™”λ©λ‹ˆλ‹€.

  2. λ‹€μŒμœΌλ‘œ render() λ©”μ„œλ“œκ°€ ν˜ΈμΆœλ˜μ–΄ λ¦¬μ•‘νŠΈ μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

  3. ComponentDidMount()λŠ” μ»΄ν¬λ„ŒνŠΈκ°€ DOM에 μ„±κ³΅μ μœΌλ‘œ 마운트된 ν›„ ν˜ΈμΆœλ˜λŠ” 생λͺ…μ£ΌκΈ° λ©”μ„œλ“œμž…λ‹ˆλ‹€.

useState

useStateλŠ” μƒνƒœλ₯Ό κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ API둜, λ‹€μŒ 두 κ°€μ§€ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.

  • μƒνƒœλ³€μˆ˜(state variable)λŠ” λžœλ” 간에 μœ μ§€λ˜λŠ” λ³€μˆ˜ κ·Έ 자체이며 μƒνƒœλ³€μˆ˜λΌ 호칭 ν•©λ‹ˆλ‹€.

  • μƒνƒœ μ„€μ • ν•¨μˆ˜(state setter function)λŠ” λ³€μˆ˜μ˜ 값을 λ°”κΎΈκ³  λ¦¬μ•‘νŠΈκ°€ ν•΄λ‹Ή μš”μ†Œμ˜ λžœλ”λ§μ„ λ°œν™” ν•©λ‹ˆλ‹€.

μ΄λŸ¬ν•œ μƒνƒœλŠ” μš”μ†Œμ˜ 생λͺ… 주기에 따라 νŠΉμ •ν•œ 절차둜 λ™μž‘ν•©λ‹ˆλ‹€.

  1. μš”μ†Œκ°€ 처음으둜 λ Œλ”λ  λ•Œ, useState에 μ „λ‹¬λœ μ΄ˆκΉƒκ°’ 0에 따라 index의 μ‹œμž‘ 값은 0이 λ©λ‹ˆλ‹€. μ΄λŠ” [0, setIndex]둜 ν‘œν˜„λ˜λ©°, λ¦¬μ•‘νŠΈλŠ” ν•΄λ‹Ή μš”μ†Œμ˜ μ΅œμ‹  μƒνƒœλ₯Ό 0으둜 μΈμ‹ν•©λ‹ˆλ‹€.

  2. μƒνƒœλ₯Ό λ°”κΏ€ λ•Œ, μ‚¬μš©μžμ˜ μƒν˜Έμž‘μš©μœΌλ‘œ setIndexκ°€ ν˜ΈμΆœλ˜μ–΄ index의 값이 λ³€κ²½λ©λ‹ˆλ‹€. (예: 1둜 λ³€κ²½) λ¦¬μ•‘νŠΈλŠ” 이 변경을 κ°μ§€ν•˜μ—¬ ν•΄λ‹Ή μš”μ†Œμ˜ λ¦¬λ Œλ”λ§μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.

  3. μš”μ†Œκ°€ λ‹€μ‹œ λ Œλ”λ  λ•Œ, useState(0)이 λ‹€μ‹œ ν˜ΈμΆœλ©λ‹ˆλ‹€. λ¦¬μ•‘νŠΈλŠ” index의 λ³€κ²½λœ 값을 κΈ°μ–΅ν•˜κ³  μžˆμœΌλ―€λ‘œ, useState(0)은 [1, setIndex]λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

useState()λŠ” 후크(hook)둜 λΆ„λ₯˜λ©λ‹ˆλ‹€. ν›„ν¬λŠ” λ Œλ”λ§ μ‹œμ μ—λ§Œ μ ‘κ·Όν•  수 μžˆλŠ” νŠΉλ³„ν•œ ν•¨μˆ˜μž…λ‹ˆλ‹€.

Define and Design Your Own State

μƒνƒœ λ³€μˆ˜μ™€ κ·Έ μžλ£Œν˜•μ€ μ‚¬μš©μžκ°€ ν•„μš”μ— 따라 자유둭게 μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜ μ½”λ“œλŠ” 두 개의 μƒνƒœλ₯Ό μ„ μ–Έν•˜λŠ” μ˜ˆμ‹œλ₯Ό λ³΄μ—¬μ€λ‹ˆλ‹€.

μ½”λ“œ
// decalre states on functional component
export default function Foo(){
	const [index, setIndex] = useState(0);
	const [showMore, setShowMore] = useState(false);
	return <></>
}

// decalre states on class component
class Foo extends React.Component{
	constructor(props){
		super(props);
		this.state = {"data": null};// state is always an object
	}
	render(){// render() has to behave like a pure funcion, don't consider or make it as instance/class
		this.setState({"data": "not null"});// you can change its value using: this.setState(__new_value__)
	}
}

Understanding Context and How It differs to State

λ¬Έλ§₯(context)은 λ…Έλ“œ 트리 λ‚΄μ—μ„œ 자료λ₯Ό μ „λ‹¬ν•˜κΈ° μœ„ν•œ μš”μ†Œμž…λ‹ˆλ‹€. μ΄λŠ” ν”„λ‘­μŠ€λ‚˜ JSX와 λ‹€λ₯Έ μ›λ¦¬λ‘œ μž‘λ™ν•˜λ©°, λ³΅μž‘ν•œ λ…Έλ“œ 트리 κ΅¬μ‘°μ—μ„œλ„ μ‰½κ²Œ μžλ£Œμ— μ ‘κ·Όν•  수 있게 ν•΄μ€λ‹ˆλ‹€.

μ½”λ“œ
const MyContext = React.createContext(defaultValue);

<MyContext.Provider value={someValue}>
	{contianValue}
</MyContext.Provider>

React.createContext()

λ¬Έλ§₯을 μ‚¬μš©ν•˜λ €λ©΄ React.createContext() ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•©λ‹ˆλ‹€. 이 ν•¨μˆ˜λŠ” λ‹€μŒ 두 κ°€μ§€ 핡심 μš”μ†Œμ™€ 관련이 μžˆμŠ΅λ‹ˆλ‹€.

  • defaultValueλŠ” λ¬Έλ§₯의 μ΄ˆκΈ°κ°’μž…λ‹ˆλ‹€. ꡬ체적인 μ΄ˆκΈ°κ°’μ„ μ •ν•˜κΈ° μ–΄λ €μš΄ 경우 null을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • ContextλŠ” createContextκ°€ λ°˜ν™˜ν•˜λŠ” κ°μ²΄μž…λ‹ˆλ‹€. λ¬Έλ§₯ μžμ²΄λŠ” 정보λ₯Ό λ‹΄κ³  μžˆμ§€ μ•ŠμœΌλ©°, λ‹€μŒκ³Ό 같은 ꡬ성 μš”μ†Œλ₯Ό κ°€μ§‘λ‹ˆλ‹€:

    • Context.ProviderλŠ” μš”μ†Œλ“€μ˜ λ¬Έλ§₯값을 κ΄€λ¦¬ν•©λ‹ˆλ‹€. λͺ¨λ“  ν•˜μœ„ μš”μ†Œλ“€μ€ 이 Provider λ‚΄λΆ€μ—μ„œ ν•΄λ‹Ή λ¬Έλ§₯값에 μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    • useContext(Context)λŠ” λ¬Έλ§₯κ°’μ˜ λ³€ν™”λ₯Ό κ°μ§€ν•˜κ³  μš”μ†Œλ“€μ΄ 이λ₯Ό ꡬ독할 수 있게 ν•©λ‹ˆλ‹€. 단, μ½”λ“œμ˜ 가독성을 ν•΄μΉ˜κ³  였λ₯˜λ₯Ό μœ λ°œν•  수 μžˆμ–΄ ꢌμž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

  • SomeContext.Providerλ₯Ό 톡해 λ¬Έλ§₯ 값을 μ§€μ •ν•˜κ³  useContext(SomeContext)λ₯Ό 톡해 μ›ν•˜λŠ” 값을 μ–»μŠ΅λ‹ˆλ‹€.

컨슈머 μ»΄ν¬λ„ŒνŠΈμ™€ useContext

λ¬Έλ§₯ 값에 μ ‘κ·Όν•˜λ €λŠ” μ»΄ν¬λ„ŒνŠΈλŠ” consumer componentλ₯Ό μ‚¬μš©ν•˜κ±°λ‚˜ useContext 후크λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

//Creating a Context and provides to the children
const ThemeContext = createContext(null);
export default function MyApp(){
	return(
		<ThemeContext.Provider value="dark">
			<Form/>
		</THemeContext.Provider>
	);
}

function Form(){
	return(<Panel/>);
}
function Panel(){
	const theme = useContext(ThemeContext);
	return(
		<div>
			{theme}
		</div>
	);
}
PreviousReact Component: How They are Rendered and Behave in BrowserNextDesign Pattern for Higher React Programming

Last updated 4 days ago