반응형
App.js
import React, { useRef, useState } from "react";
import Hello from "./Hello.js";
import "./App.css";
import Wrapper from "./Wrapper.js";
import Counter from "./Counter.js";
import InputSample from "./InputSample.js";
import UserList from "./UserList.js";
import CreateUser from "./CreateUser.js";
function App() {
const [inputs, setInputs] = useState({
username: "",
email: ""
});
const { username, email } = inputs;
const onChange = e => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value
});
};
const [users, setUsers] = useState([
{
id: 1,
username: "velopert",
email: "public.velopert@gmail.com"
},
{
id: 2,
username: "tester",
email: "tester@example.com"
},
{
id: 3,
username: "liz",
email: "liz@example.com"
}
]);
const nextId = useRef(4);
const onCreate = () => {
const user = {
id: nextId.current,
username,
email
};
setUsers([...users, user]);
setInputs({
username: "",
email: ""
});
nextId.current += 1;
};
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} />
</>
);
}
export default App;
CreateUser.js
import React from "react";
//4가지의 props를 받아옴
function CreateUser({ username, email, onChange, onCreate }) {
return (
<div>
<input
name="username"
placeholder="계정명"
onChange={onChange}
value={username}
/>
<input
name="email"
placeholder="이메일"
onChange={onChange}
value={email}
/>
<button onClick={onCreate}>등록</button>
</div>
);
}
export default CreateUser;
출처 : 벨로퍼트님의 강의 예제입니다.
반응형
'2020 > React' 카테고리의 다른 글
React 배열의 항목 수정하기 (0) | 2020.04.01 |
---|---|
react #배열 항목 제거하기 (0) | 2020.04.01 |
React 배열 렌더링하기 (0) | 2020.03.31 |
React useRef로 Dom 선택하기 (0) | 2020.03.31 |
InputSample (0) | 2020.03.30 |