更新记录时尝试显示输入列表时出现问题
Problem trying to display a list of inputs when updating a record
我正在练习使用 spring 引导进行反应,当我想更新信息时呈现输入有问题
const {id} = useParams();
const dispatch = useDispatch();
const [inputList, setInputList] = useState([{id: '', mobileNumber: "", supplierNumber: id}]);
const history = useHistory();
//React Hook form estructura
const {register, handleSubmit, setValue, formState: { errors }} = useForm({resolver: yupResolver(schema)});
useEffect(() => {
//Si existe un id en el parametro de la url entonces realizamos el fetch
if(id) {
const fetchData = async () => {
try {
const resp = await axios.get(`${SUPPLIERS_ENDPOINT}/${id}`);
if(resp.data !== null) {
const {name, nif, email, webPage, mobileNumbers} = resp.data;
//Con estos setValues seteamos los values de los inputs del formulario, para que aparezcan rellenos con la informacion que nos devolvio el metodo get
setValue("name", name);
setValue("nif", nif);
setValue("webPage", webPage);
setValue("email", email);
*****I think the problem is here*************
mobileNumbers.map((m,i) => {
if(i !== 0) {
setInputList([...inputList, {id: m.id, mobileNumber: m.mobileNumber, supplierNumber: id}]);
} else {
setInputList([{id: m.id, mobileNumber: m.mobileNumber, supplierNumber: id}]);
}
});
}
} catch (error) {
setErrores(error);
console.log(error);
}
}
fetchData();
}
}, []);
问题是 useState 只保存了 setInputList 的初始值和最后一个值。
输出:
但我想要的输出是这样的
0: {id: 119, mobileNumber: 951, supplierNumber: "117"}
1: {id: 118, mobileNumber: 6789, supplierNumber: "117"}
已解决!它只是在 useEffect 中对此进行了更改:
mobileNumbers.forEach((m,i) => {
if(i !== 0) {
setMobNumsList(i => [...i, {id: m.id, mobileNumber: m.mobileNumber,supplierNumber:id}]);
} else {
setMobNumsList([{id: m.id, mobileNumber: m.mobileNumber, supplierNumber: id}]);
}
});
我正在练习使用 spring 引导进行反应,当我想更新信息时呈现输入有问题
const {id} = useParams();
const dispatch = useDispatch();
const [inputList, setInputList] = useState([{id: '', mobileNumber: "", supplierNumber: id}]);
const history = useHistory();
//React Hook form estructura
const {register, handleSubmit, setValue, formState: { errors }} = useForm({resolver: yupResolver(schema)});
useEffect(() => {
//Si existe un id en el parametro de la url entonces realizamos el fetch
if(id) {
const fetchData = async () => {
try {
const resp = await axios.get(`${SUPPLIERS_ENDPOINT}/${id}`);
if(resp.data !== null) {
const {name, nif, email, webPage, mobileNumbers} = resp.data;
//Con estos setValues seteamos los values de los inputs del formulario, para que aparezcan rellenos con la informacion que nos devolvio el metodo get
setValue("name", name);
setValue("nif", nif);
setValue("webPage", webPage);
setValue("email", email);
*****I think the problem is here*************
mobileNumbers.map((m,i) => {
if(i !== 0) {
setInputList([...inputList, {id: m.id, mobileNumber: m.mobileNumber, supplierNumber: id}]);
} else {
setInputList([{id: m.id, mobileNumber: m.mobileNumber, supplierNumber: id}]);
}
});
}
} catch (error) {
setErrores(error);
console.log(error);
}
}
fetchData();
}
}, []);
问题是 useState 只保存了 setInputList 的初始值和最后一个值。
输出:
但我想要的输出是这样的
0: {id: 119, mobileNumber: 951, supplierNumber: "117"}
1: {id: 118, mobileNumber: 6789, supplierNumber: "117"}
已解决!它只是在 useEffect 中对此进行了更改:
mobileNumbers.forEach((m,i) => {
if(i !== 0) {
setMobNumsList(i => [...i, {id: m.id, mobileNumber: m.mobileNumber,supplierNumber:id}]);
} else {
setMobNumsList([{id: m.id, mobileNumber: m.mobileNumber, supplierNumber: id}]);
}
});