๊ด€๋ฆฌ ๋ฉ”๋‰ด

<Hello Hosung๐Ÿ˜Ž/>

[React, RN] ๋ฆฌ์•กํŠธ ์‹œ์ž‘ํ•˜๊ธฐ๐Ÿ˜€ ๋ณธ๋ฌธ

๐Ÿ“ฑ JavaScript/ใ…คReact, RN

[React, RN] ๋ฆฌ์•กํŠธ ์‹œ์ž‘ํ•˜๊ธฐ๐Ÿ˜€

์ขŒ์ถฉ์šฐ๋Œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ผ๊ธฐ๐Ÿง 2024. 11. 21. 13:42

1. ๋ฆฌ์•กํŠธ ์†Œ๊ฐœ

๋ฆฌ์•กํŠธ๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ UI๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉฐ, ์ƒํƒœ ๊ด€๋ฆฌ์™€ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

2. JSX (JavaScript XML)

JSX๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์•ˆ์—์„œ HTML๊ณผ ๋น„์Šทํ•œ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.

const element = <h1>Hello, world!</h1>;

 

* JSX๋Š” React.createElement๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

 

3. ์ปดํฌ๋„ŒํŠธ (Component)

๋ฆฌ์•กํŠธ์—์„œ UI๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋ˆ„์–ด ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

 

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

 

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

 

 

 

4. Props (์†์„ฑ)

props๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

props๋Š” ์ฝ๊ธฐ ์ „์šฉ์ด๋ฉฐ, ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

 

 

 

5. State (์ƒํƒœ)

state๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” this.state๋กœ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useState ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ์˜ ์ƒํƒœ

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </div>
    );
  }
}

 

 

 

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ์˜ ์ƒํƒœ (useState ํ›…)

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

 

6. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง

๋ฆฌ์•กํŠธ์—์„œ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ JSX์˜ ์†์„ฑ์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ด๋ฆ„์€ camelCase๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

function MyButton() {
  const handleClick = () => {
    alert("Button clicked!");
  };
  return <button onClick={handleClick}>Click me</button>;
}

 

7. ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง

๋ฆฌ์•กํŠธ์—์„œ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง์„ ํ†ตํ•ด ์ƒํƒœ๋‚˜ props์— ๋”ฐ๋ผ UI๋ฅผ ๋™์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function LoginButton(props) {
  if (props.isLoggedIn) {
    return <button>Logout</button>;
  }
  return <button>Login</button>;
}

 

8. ๋ฆฌ์ŠคํŠธ์™€ ํ‚ค (List and Keys)

๋ฆฌ์•กํŠธ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ ๊ฐ ํ•ญ๋ชฉ์— key ์†์„ฑ์„ ๋ถ€์—ฌํ•ด์•ผ ํšจ์œจ์ ์ธ ์—…๋ฐ์ดํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

function NumberList(props) {
  const numbers = props.numbers;
  const listItems = numbers.map((number) =>
    <li key={number.toString()}>{number}</li>
  );
  return <ul>{listItems}</ul>;
}

 

9. ์ปดํฌ๋„ŒํŠธ ๋ผ์ดํ”„์‚ฌ์ดํด (Lifecycle)

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๋™์•ˆ ํŠน์ • ๋™์ž‘์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class MyComponent extends React.Component {
  componentDidMount() {
    console.log('Component did mount!');
  }

  render() {
    return <div>Hello</div>;
  }
}

 

10. ํ›… (Hooks)

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ์™€ ๋ผ์ดํ”„์‚ฌ์ดํด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฆฌ์•กํŠธ์˜ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

useState: ์ƒํƒœ ๊ด€๋ฆฌ

useEffect: ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ ์ฒ˜๋ฆฌ

useContext: ์ปจํ…์ŠคํŠธ ์‚ฌ์šฉ

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const intervalId = setInterval(() => setSeconds(seconds + 1), 1000);
    return () => clearInterval(intervalId);
  }, [seconds]);

  return <h1>Time: {seconds}s</h1>;
}

 

 

 

11. ์ปจํ…์ŠคํŠธ (Context)

๋ฆฌ์•กํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ ์ „์—ญ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ Context๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

const ThemeContext = React.createContext('light');

function ThemedComponent() {
  return (
    <ThemeContext.Consumer>
      {theme => <div>The current theme is {theme}</div>}
    </ThemeContext.Consumer>
  );
}

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <ThemedComponent />
    </ThemeContext.Provider>
  );
}

 

12. ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ (React Router)

๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŽ˜์ด์ง€ ๊ฐ„์˜ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/home" component={Home} />
        <Route path="/about" component={About} />
      </Switch>
    </Router>
  );
}

 

๋งˆ๋ฌด๋ฆฌ

๋ฆฌ์•กํŠธ๋Š” ํšจ์œจ์ ์ธ UI ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๋กœ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•๊ณผ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์ด ์ž˜ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฌธ๋ฒ•์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฆฌ์•กํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.