| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
- oauth
- ๋์์ธํจํด
- @scheduler
- ์ฝํ
- spring scheduler
- ์ค๋ธ์
- GoogleLogin
- Node.js
- dockerhub
- MySQL
- CICD
- db์ ๋ต
- node.js mysql ์ฐ๋
- ์๊ณ ๋ฆฌ์ฆ
- ์๋ฐ
- java์ฑํ
- React
- Personal Access Token
- ์ฝ๋ฉํ ์คํธ
- ์ฟ ๋ฒ๋คํฐ์ค
- ๋ ผ๋ธ๋กํน
- spring
- injection๊ณต๊ฒฉ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ํฌ๋ก์ค์ฌ์ดํธ ์คํฌ๋ฆฝํธ
- SpringBoot
- db์ํคํ ์ณ
- docker
- Jenkins
- Java
- Today
- Total
<Hello Hosung๐/>
[React, RN] ๋ฆฌ์กํธ ์์ํ๊ธฐ๐ ๋ณธ๋ฌธ
[React, RN] ๋ฆฌ์กํธ ์์ํ๊ธฐ๐
์ข์ถฉ์ฐ๋ ๋ฐฑ์๋ ๊ฐ๋ฐ์ ์ผ๊ธฐ๐ง 2024. 11. 21. 13:421. ๋ฆฌ์กํธ ์๊ฐ
๋ฆฌ์กํธ๋ ์ฌ์ฉ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌ์ถํ๊ธฐ ์ํ ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
์ปดํฌ๋ํธ ๊ธฐ๋ฐ์ผ๋ก 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 ๊ฐ๋ฐ์ ์ํ ๊ฐ๋ ฅํ ๋๊ตฌ๋ก, ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ๋ฒ๊ณผ ๋ค์ํ ๊ธฐ๋ฅ๋ค์ด ์ ๊ฒฐํฉ๋์ด ์์ต๋๋ค. ์์์ ์ค๋ช ํ ๋ฌธ๋ฒ์ ๋ฐํ์ผ๋ก ๋ฆฌ์กํธ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๋ฉด์ ๋ ๋ง์ ๊ธฐ๋ฅ์ ํ์ฉํ ์ ์์ต๋๋ค.
'๐ฑ JavaScript > ใ คReact, RN' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Expo + React Native] 1. ํ๋ก์ ํธ ์ด๊ธฐ ๊ตฌ์กฐ ์๋ฒฝ ์ดํด (0) | 2025.11.30 |
|---|---|
| [React, RN] ํจ์จ์ ์ธ ๋ ๋๋ง๊ณผ ๋น ๋ฅธ ๋ก๋ฉ์ ์ํ ์ ๋ต (1) | 2024.11.25 |
| [React, RN] ๋ฆฌ์กํธ ๋ผ์ฐํฐ ์์ ์ ๋ณต (1) | 2024.11.24 |
| [React, RN] ๋ฆฌ์กํธ ํ (React Hooks) ์๊ฐ๐ (0) | 2024.11.22 |