React에서 component에서 하위 component로 데이터를 전송 하고 싶을때 사용하는 방법이다.
tag 내부에 Attribute에 아래와 같이 전달 가능하며 tag 사이에 넣어서도 전달 가능하다.
//App.js
function App() {
console.log("App");
return (
<div className='ViewGood'>
<Header02/>
<View02 data1="[props로]" data2="[상위에서]">[하위로 데이터전송]</View02>
<Footer02/>
</div>
);
}
export default App;
//View02.js
const View02 = (props) => {
console.log("view02");
return (
<div className='ViewGood'>
<h1>[View02 Component]</h1>
<ul>
상위컴포넌트 데이터받기 : data1 = {props.data1} data2 = {props.data2}
상위컴포넌트 데이터받기 : children={props.children}
</ul>
</div>
);
};
export default View02;
//userState를 통해 product와 users object를 생성하고, setFunction 생성
function App() {
const[no, setNo] = useState(1);
const[product, setProduct] = useState(
{no:100, name:'tv'}
);
const [users, setUsers] = useState(
[{id:'user1', pwd:1},
{id:'user2', pwd:2}]
);
return (
<div className='ViewGood'>
<h1>03state연습</h1>
<li>{no}</li>
<li>{product.no} {product.name}</li>
<li>{users[0].id} {users[0].pwd}</li>
<li>{users[1].id} {users[1].pwd}</li>
<Header031 no={no} product={product} users={users}></Header031><hr/>
<View02 data1="[props로]" data2="[상위에서]">[하위로 데이터전송]</View02><hr/>
<Footer02/>
</div>
);
}
function App() {
const[no, setNo] = useState(1);
const[product, setProduct] = useState(
{no:100, name:'tv'}
);
const [users, setUsers] = useState(
[{id:'user1', pwd:1},
{id:'user2', pwd:2}]
);
const changeNo =()=>{
console.log("App changeNo");
setNo(no+1);
}
const changeProduct=()=>{
console.log("App changeProduct");
let newNo = product.no + 1;
let newName = product.name + newNo;
let newProduct = {no:newNo, name:newName};
setProduct(newProduct);
}
const changeUsers=()=>{
console.log("App changUsers");
let newUser = [{id:'user3', pwd:3}];
let newUsers = [...users, ...newUser];
}
return (
<div className='ViewGood'>
<h1>03state연습</h1>
<li>{no}</li>
<button onClick={changeNo}>숫자 1씩 증가</button>
<button onClick={changeProduct}>상품 정보 수정</button>
<button onClick={changeUsers}>회원정보 추가</button>
</div>
);
}
export default App;
function App() {
const[no, setNo] = useState(1);
const[product, setProduct] = useState(
{no:100, name:'tv'}
);
const [users, setUsers] = useState(
[{id:'user1', pwd:1},
{id:'user2', pwd:2}]
);
const changeNo =()=>{
console.log("App changeNo");
setNo(no+1);
}
const changeProduct=()=>{
console.log("App changeProduct");
let newNo = product.no + 1;
let newName = product.name + newNo;
let newProduct = {no:newNo, name:newName};
setProduct(newProduct);
}
const changeUsers=()=>{
console.log("App changUsers");
let newUser = [{id:'user3', pwd:3}];
let newUsers = [...users, ...newUser];
setUsers(newUsers);
}
return (
<div className='ViewGood'>
<li>{no}</li>
<button onClick={changeNo}>숫자 1씩 증가</button>
<button onClick={changeProduct}>상품 정보 수정</button>
<button onClick={changeUsers}>회원정보 추가</button>
{/* 하위 component에 function 전달*/}
<Header043 no={no} product={product} users={users} changeProduct={changeProduct} changeNo={changeNo} changeUsers={changeUsers}></Header043>
</div>
);
}
export default App;
const Header043 = (props) => {
console.log("Header043");
console.log(props);
const[no,setNo] = useState(props.no);
const[product,setProduct] = useState(props.product);
const[users,setUsers] = useState(props.users);
const parentsChangeNo=()=>{
props.changeNo();
}
const parentsChangeProduct=()=>{
props.changeProduct();
}
const parentsChangeUsers=()=>{
props.changeUsers();
}
return (
<header className='ViewGood'>
<h1>[Header042 Component]</h1>
<li>{no}</li>
<button onClick={parentsChangeNo}>숫자 1씩 증가</button>
{productView}
<button onClick={parentsChangeProduct}>상품 정보 수정</button>
{usersView}
<button onClick={parentsChangeUsers}>회원정보 추가</button>
</header>
);
};
export default Header043;
//component에서 사용 할 context를 생성한다.
import { createContext } from "react";
console.log("Context05");
const NoContext = createContext(1);
export default NoContext;
const ProductContext = createContext({no:100, name:'tv'});
export {ProductContext};
export const UsersContext = createContext([{id:'user1', pwd:1}, {id:'user2', pwd:2}]);
import { useContext} from "react";
import ViewOther from "./ViewOther05";
import NoContext, {UsersContext, ProductContext} from "./Context05";
const View05=()=>{
console.log("View05");
//userContext를 통해 context의 값을 가져온다
const no = useContext(NoContext);
const product = useContext(ProductContext);
const users = useContext(UsersContext);
return(
<div className="ViewGood">
<h1>[View05 Component]</h1>
<br/>
no : {no}<br/><br/>
product.no : {product.no} product.name : {product.name}
{
users.map((user, index)=>{
return <li key={index}>{users[index].id} {users[index].pwd}</li>
})
}
<ViewOther/>
</div>
);
}
export default View05;