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

Design Pattern for Higher React Programming

이 λ¬Έμ„œλŠ” 리앑 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©λ˜λŠ” μ£Όμš” λ””μžμΈ νŒ¨ν„΄λ“€μ„ μ„€λͺ…ν•©λ‹ˆλ‹€. μ£Όμš” λ‚΄μš©μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:

  • Container와 Presentation νŒ¨ν„΄: λΉ„μ¦ˆλ‹ˆμŠ€ 둜직과 UIλ₯Ό λΆ„λ¦¬ν•˜μ—¬ κ΄€λ¦¬ν•˜λŠ” 방법을 μ„€λͺ…ν•©λ‹ˆλ‹€.

  • Container Component: 데이터 μ²˜λ¦¬μ™€ μƒνƒœ 관리λ₯Ό λ‹΄λ‹Ήν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμ˜ μ—­ν• κ³Ό κ΅¬ν˜„ 방법을 λ‹€λ£Ήλ‹ˆλ‹€.

  • Presentational Component: UI ν‘œν˜„μ— μ§‘μ€‘ν•˜λ©° propsλ₯Ό 톡해 데이터λ₯Ό λ°›μ•„ ν‘œμ‹œν•˜λŠ” 방식을 μ„€λͺ…ν•©λ‹ˆλ‹€.

  • κ³ κΈ‰ νŒ¨ν„΄: HOC(Higher Order Component)와 Render Propsλ₯Ό ν†΅ν•œ μ»΄ν¬λ„ŒνŠΈ 둜직 μž¬μ‚¬μš© 방법을 μ†Œκ°œν•©λ‹ˆλ‹€.

  • Hook 기반 뢄리: μ»€μŠ€ν…€ 훅을 μ‚¬μš©ν•˜μ—¬ λ‘œμ§μ„ λΆ„λ¦¬ν•˜κ³  μž¬μ‚¬μš©ν•˜λŠ” ν˜„λŒ€μ μΈ μ ‘κ·Ό 방식을 μ„€λͺ…ν•©λ‹ˆλ‹€.

Container/Representational Component

μ½”λ“œμ˜ 효율적인 관리λ₯Ό μœ„ν•΄ μ»¨ν…Œμ΄λ„ˆμ™€ λ Œλ” μ»΄ν¬λ„ŒνŠΈλ₯Ό λΆ„λ¦¬ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. μ΄λŠ” μœ μ§€λ³΄μˆ˜μ„±μ„ 높이고 μ»΄ν¬λ„ŒνŠΈμ˜ 역할을 λͺ…ν™•νžˆ ν•˜λ©°, 특히 λŒ€κ·œλͺ¨ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ½”λ“œμ˜ ν™•μž₯μ„±κ³Ό ν…ŒμŠ€νŠΈκ°€ μš©μ΄ν•΄μ§‘λ‹ˆλ‹€.

μ»¨ν…Œμ΄λ„ˆ μ»΄ν¬λ„ŒνŠΈ(container component)λŠ” 논리, μƒνƒœ 관리, 데이터 페칭 등을 μ²˜λ¦¬ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€. APIμ™€μ˜ μƒν˜Έμž‘μš©μ„ 톡해 얻은 데이터와 콜백 ν•¨μˆ˜λ₯Ό μžμ‹ μ»΄ν¬λ„ŒνŠΈμ— μ „λ‹¬ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€. 단, μ‹œκ°μ  ν‘œν˜„μ΄λ‚˜ κ΄€λ ¨ λ‘œμ§μ€ 직접 닀루지 μ•ŠμŠ΅λ‹ˆλ‹€.

function UserContainer() {
	const [users, setUsers] = useState()
	useEffect(() => {
		fetch('/api/users')
		.then((res) => res.json())
		.then((data) => setUsers(data))
	}, [])
	
	return <UserList users={users}/>
}

ν”„λ ˆμ  ν…Œμ΄μ…”λ„ μ»΄ν¬λ„ŒνŠΈ(presentational component)λŠ” μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€μ™€ μ‹œκ°μ  ν‘œν˜„μ„ λ‹΄λ‹Ήν•˜λŠ” μ»΄ν¬λ„ŒνŠΈλ‘œ, 자체적인 μƒνƒœ 관리 없이 propsμ—λ§Œ μ˜μ‘΄ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ΄λŸ¬ν•œ propsλŠ” μƒμœ„ μ»¨ν…Œμ΄λ„ˆ μ»΄ν¬λ„ŒνŠΈλ‘œλΆ€ν„° μ „λ‹¬λ°›μŠ΅λ‹ˆλ‹€.

const UserList = ({ users }) => {
  return (
    <ul>
      {users.map((user) => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
};

HOC, High Order Class

μ»΄ν¬λ„ŒνŠΈλ₯Ό μž…λ ₯λ°›μ•„ μƒˆλ‘œμš΄ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ‘œ, μ»¨ν…Œμ΄λ„ˆ μ»΄ν¬λ„ŒνŠΈλ“€ μ‚¬μ΄μ—μ„œ κ³΅ν†΅λœ λ‘œμ§μ„ μž¬μ‚¬μš©ν•˜λŠ” 데 νš¨κ³Όμ μž…λ‹ˆλ‹€.

const withUserData = (WrappedComponent) => {
  return () => {
    const [users, setUsers] = useState([]);

    useEffect(() => {
      fetch('/api/users')
        .then((res) => res.json())
        .then((data) => setUsers(data));
    }, []);

    return <WrappedComponent users={users} />;
  };
};

const UserListWithData = withUserData(UserList);

Render Props

μš”μ†Œ κ°„μ˜ λ‘œμ§μ„ κ³΅μœ ν•˜κΈ° μœ„ν•œ νŠΉλ³„ν•œ ν•¨μˆ˜ νŒ¨ν„΄μœΌλ‘œ, μ»΄ν¬λ„ŒνŠΈκ°€ μžμ‹ μ˜ λ Œλ”λ§ λ‘œμ§μ„ λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ™€ κ³΅μœ ν•  수 μžˆλ„λ‘ props둜 ν•¨μˆ˜λ₯Ό μ „λ‹¬ν•˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€. 이λ₯Ό 톡해 μ»΄ν¬λ„ŒνŠΈλŠ” μžμ‹ μ˜ λ™μž‘μ„ μœ μ—°ν•˜κ²Œ ν™•μž₯ν•˜κ³  μž¬μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const UserProvider = ({ render }) => {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    fetch('/api/users')
      .then((res) => res.json())
      .then((data) => setUsers(data));
  }, []);

  return render(users);
};

const UserContainer = () => {
  return (
    <UserProvider
      render={(users) => <UserList users={users} />}
    />
  );
};

Hook for Separation

논리와 μƒνƒœλ₯Ό μΊ‘μŠν™”ν•˜κΈ° μœ„ν•΄ μ‚¬μš©μž μ •μ˜ 후크(custom hook)λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό 톡해 μ»΄ν¬λ„ŒνŠΈμ˜ 가독성과 ꡬ쑰λ₯Ό κ°œμ„ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const useUsers = () => {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    fetch('/api/users')
      .then((res) => res.json())
      .then((data) => setUsers(data));
  }, []);

  return users;
};

const UserContainer = () => {
  const users = useUsers();
  return <UserList users={users} />;
};
PreviousState and Context: A Key Function to Operate the React ApplicationNextA Comprehensive Guide to Creating Memorable Melodies through Motif and Phrasing

Last updated 4 days ago