如何使用文本框上的句柄更改事件来更新反应数组状态?
How can I use handle change event on text box to update react array state?
Below is the code, where on preload all items gets bind inside text box and with the help of same text box I want to update the value of state in react.
items state get's updated but it throws an error as soon as the handlechange method execution gets over.
items.map is not a function. How can I use handle change function on text box to update array state?
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import {getItems} from "./helper/adminapicall";
const ManageItems = () => {
const [items, setItems] = useState([]);
const preload = () => {
getItems().then((data) => {
if (data.error) {
console.log(data.error);
} else {
setItems(data);
}
});
};
const handleChange = (index, name) => (event) => {
setItems({
...items[index].name,
error: false,
[name]: event.target.value,
});
console.log(items);
};
useEffect(() => {
preload();
}, []);
return (
<Base title="Welcome admin" description="Manage items here">
<h2 className="mb-4">All items:</h2>
<Link className="btn btn-info" to={`/admin/dashboard`}>
<span className="">Admin Home</span>
</Link>
<div className="row">
<div className="col-12">
<h2 className="text-center text-white my-3">Total 3
products</h2>
{items &&
items.map((item, index) => {
return (
<div className="row form-group" key={index}>
<div className="col-4">
<input
type="text"
className="form-control"
value={item.name}
onChange={handleChange(index, "name")}
/>
</div>
</div>
);
})}
</div>
</div>
</Base>
);``
};
export default ManageItems;
onchange={e=> setItems(e.target.value)}
我希望这就是您在问题中提出的意思。
Below is the code, where on preload all items gets bind inside text box and with the help of same text box I want to update the value of state in react. items state get's updated but it throws an error as soon as the handlechange method execution gets over. items.map is not a function. How can I use handle change function on text box to update array state?
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import {getItems} from "./helper/adminapicall";
const ManageItems = () => {
const [items, setItems] = useState([]);
const preload = () => {
getItems().then((data) => {
if (data.error) {
console.log(data.error);
} else {
setItems(data);
}
});
};
const handleChange = (index, name) => (event) => {
setItems({
...items[index].name,
error: false,
[name]: event.target.value,
});
console.log(items);
};
useEffect(() => {
preload();
}, []);
return (
<Base title="Welcome admin" description="Manage items here">
<h2 className="mb-4">All items:</h2>
<Link className="btn btn-info" to={`/admin/dashboard`}>
<span className="">Admin Home</span>
</Link>
<div className="row">
<div className="col-12">
<h2 className="text-center text-white my-3">Total 3
products</h2>
{items &&
items.map((item, index) => {
return (
<div className="row form-group" key={index}>
<div className="col-4">
<input
type="text"
className="form-control"
value={item.name}
onChange={handleChange(index, "name")}
/>
</div>
</div>
);
})}
</div>
</div>
</Base>
);``
};
export default ManageItems;
onchange={e=> setItems(e.target.value)}
我希望这就是您在问题中提出的意思。