// src/config.js
export const API = "http://localhost:8000";
// App.js
fetch(`${API}/product/1`);
// DON'T
const fourRuleCalculate = (event, a, b) => {
const { innerText } = event.target;
if (innerText === "+") {
return a + b
} else if (innerText === "-") {
return a - b
} else if (innerText === "*") {
return a * b
} else if (innerText === "/") {
return a / b
}
}
// DO
const sum = (a, b) => a + b
const minus = (a, b) => a - b
const multiply = (a, b) => a * b
const divide = (a, b) => a / b
console.log("안녕하세요, " + "위코더" + "!");
function sayHello(person) {
console.log("안녕하세요, " + person + "!");
}
sayHello("위코더");
function saySomething(message, person) {
console.log(message + ", " + person + "!");
}
saySomething("안녕하세요", "위코더");
saySomething("안녕히가세요", "위코더");
function saySomething(message, people) {
const peopleArr = Array.isArray(people) ? people : [people]
for (const person of peopleArr) {
console.log(message + ", " + person + "!");
}
}
saySomething("안녕하세요", "위코더1")
saySomething("안녕하세요", ["위코더1", "위코더2", "위코더3"])
const foo = "속성"
const data = {
["계산된" + foo + "명"]: "객체 계산된 속성명 문법!"
}
console.log(data["계산된속성명"])
// "객체 계산된 속성명 문법!"
const sayHello = (message = "안녕하세요!") => {
return message
}
sayHello() // "안녕하세요!"
sayHello("안녕히 가세요!") // "안녕히 가세요!"
handleId = (e) => {
this.setState({ id: e.target.value });
};
handleName = (e) => {
this.setState({ name: e.target.value });
};
handleEmail = (e) => {
this.setState({ email: e.target.value });
};
handleAddress = (e) => {
this.setState({ address: e.target.value });
};
=>
const handleInput = (e) => {
const { name, value } = e.target;
**// 객체 계산된 속성명**
this.setState({ [name]: value });
};
fetch("http://localhost:8000", {
Authorization: localstorage.getItem("access_token")
})
fetch("http://localhost:8000")
.then(res => res.json())
.then(res => localStorage.setItem("access_token", res.token))
=>
const ACCESS_TOKEN_KEY = "access_token";
export const getToken = () => {
const token = localstorage.getItem(ACCESS_TOKEN_KEY);
if (token) {
return token;
} else {
alert("토큰이 존재하지 않습니다!");
return "";
}
}
export const setToken = token => {
localstorage.setItem(ACCESS_TOKEN_KEY, token)
}
export const tokenUtils = {
get: getToken,
set: setToken
}
fetch("http://localhost:8000", {
Authorization: tokenUtils.get()
})
fetch("http://localhost:8000")
.then(res => res.json())
.then(res => tokenUtils.set(res.token))
const Form = () => {
const [id, setId] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isAllValid, setIsAllValid] = useState(false);
const handleId = (e) => {
setId(e.target.value);
}
const handleEmail = (e) => {
setEmail(e.target.value);
}
const handlePassword = (e) => {
setPassword(e.target.value);
}
useEffect(() => {
if (id.length > 6 && email.includes("@") && password.length > 8) {
setIsAllValid(true);
} else {
setIsAllValid(false);
}
}, [id, email, password]);
return (
<div>
<input type="text" name="id" onChange={handleId} />
<input type="email" name="email" onChange={handleEmail} />
<input type="password" name="password" onChange={handlePassword} />
<button disabled={!isAllChecked} />
</div>
)
}
=>
handleId
, handleEmail
, handlePassword
)id.length > 6 && email.includes("@") && password.length > 8
)// Form.js
const Form = () => {
const [formValues, setFormValues] = useState({
id: "",
email: "",
password: ""
});
const handleForm = e => {
const { name, value } = e.target;
**// 스프레드 연산자, 계산된 속성명**
setFormValues({ ...formValues, [name]: value });
}
return (
<div>
<input type="text" name="id" onChange={handleForm} />
<input type="email" name="email" onChange={handleForm} />
<input type="password" name="password" onChange={handleForm} />
<button disabled={!isAllValid(formValues)} />
</div>
)
}
// validator.js
export const validator = {
//각각의 유효성 검사 함수
id: id => id.length > 6,
email: email => email.includes("@"),
password: password => password.length > 10
}
// ver.1
export const isAllValid = form =>
Object.entries(form).every(([key, value]) => validator[key](value))
// ver.2
export const isAllValid = form => {
for (const name in form) {
const value = form[name];
const validateFunction = validator[name]
if (!validateFunction(value)) return false;
}
return true;
}
//formValues가 모두 유효한지 검사하는 함수 //순회 돌면서 각각에 맞는 유효성 검사 함수 호출 //모든 유효성 검사가 true->isAllValid=>true 이중에 하나라도 false면 false //const formValues = { id: '', email:'', password: '', }; const isAllValid = (formValues) => { for(const key in formValues) { constole.log('key',key); ...... } }