ref) https://react.vlpt.us/basic/01-concept.html
Component 기반 구조
SPA(Single Page Application)
Virtual DOM
Props and State
props 와 state는 리액트에서 데이터를 사용할 때 다루는 개념
react에서는 UI의 데이터는 반드시 props와 state로 관리되어야 한다
데이터를 일관성 있게 다루기 위해서
react life cycle이 props와 state의 변경을 기준으로 화면을 갱신하기 때문
SN | Props | State |
---|---|---|
1. | Props are read-only. | State changes can be asynchronous. |
2. | Props are immutable. | State is mutable. |
3. | Props allow you to pass data from one component to other components as an argument. | State holds information about the components. |
4. | Props can be accessed by the child component. | State cannot be accessed by child components. |
5. | Props are used to communicate between components. | States can be used for rendering dynamic changes with the component. |
6. | Stateless component can have Props. | Stateless components cannot have State. |
7. | Props make components reusable. | State cannot make components reusable. |
8. | Props are external and controlled by whatever renders the component. | The State is internal and controlled by the React Component itself. |
Props
State
JSX
{...javascript expression...}
회원 정보를 입력받아 목록에 추가하는 예제
import {TotalUserInform} from "./components/total-user-inform";
function App() {
return (
<div>
<TotalUserInform/>
</div>
);
}
export default App;
import {UserInform} from "./user-inform";
import {UserList} from "./user-list";
import {useState} from "react";
export const TotalUserInform = () => {
const [userList, setUserList] = useState([]);
const [prevUser, setPrevUser] = useState(null);
const [user, setUser] = useState(null);
if (user !== prevUser){
setUserList([...userList, user])
setPrevUser(user)
}
return (<div>
<UserInform setUser={setUser}/>
<UserList userList={userList}/>
</div>)
}
import React, {useState} from "react";
export const UserInform = ({setUser}) => {
const [inputIdValue, setInputIdValue] = useState("");
const [inputPwValue, setInputPwValue] = useState("");
const [inputNameValue, setInputNameValue] = useState("");
const [inputAgeValue, setInputAgeValue] = useState("");
const inputIdHandler = (event) => {
setInputIdValue(event.target.value)
};
const inputPwHandler = (event) => {
setInputPwValue(event.target.value)
};
const inputNameHandler = (event) => {
setInputNameValue(event.target.value)
};
const inputAgeHandler = (event) => {
setInputAgeValue(event.target.value)
};
const SubmitHandler = () => {
setUser({
id: inputIdValue,
pw: inputPwValue,
name: inputNameValue,
age: +inputAgeValue
})
}
return (
<div>
<label>Enter ID : </label>
<input id={"id"} type="text" placeholder={"ID"} onChange={inputIdHandler}/><br/>
<label>Enter PW : </label>
<input id={"pw"} type="text" placeholder={"PW"} onChange={inputPwHandler}/><br/>
<label>Enter Name : </label>
<input id={"name"} type="text" placeholder={"Name"} onChange={inputNameHandler}/><br/>
<label>Enter Age : </label>
<input id={"age"} type="text" placeholder={"Age"} onChange={inputAgeHandler}/><br/>
<button onClick={SubmitHandler}>Submit</button>
</div>
)
}
import React from "react";
export const UserList = ({userList}) => {
return (
<table>
<thead></thead>
<tbody>
{userList.map((user, index) => {
return (
<tr key={index} className="text-2xl">
<td>id: {user.id}</td>
<td>pw: {user.pw}</td>
<td>name : {user.name}</td>
<td>age: {user.age}</td>
</tr>);
})}
</tbody>
<tfoot></tfoot>
</table>
)
}