Study/React
[React] useState에서 객체 값 변경하기
성으니:)
2023. 5. 3. 14:33
코드 1
import React, { useState } from "react";
function Test() {
const [condition, setCondition] = useState({
type: "",
value: "",
});
const changeCondition = (key, value) => {
setCondition((current) => {
let newCondition = { ...current };
newCondition[key] = value;
return newCondition;
});
};
return (
...
)
}
코드 2
import React, { useState } from "react";
function Test() {
const [condition, setCondition] = useState({
type: "",
value: "",
});
const {type, value} = condition;
const onChange = e => {
const {key, value} = e.target;
setCondition({
...condition,
[key]: value
});
}
return (
...
)
}
참고 사이트
https://aldrn29.tistory.com/14