'How can I update the value from the database in React
When I edit the form here, I need to show the value in the database. so how can i get this value
const dataValue = ndata.filter((obj) => obj._id === dataid ).map((aa)=> {
return (
{aa.AddressTitle}
)
})
The datavalue comes in correctly but I can't show it as the default value in state why
const [formData, setFormData] = useState({
billingAddr: false,
shippingAddr: false,
AddressTitle: isEdit===true ? (dataValue):("")
});
i should show this as default value in AddressTitle.
UPDATE QUEST
useEffect(() => {
const api = async () => {
try {
const response = await authService.getAddr();
setndata(response?.data);
response.data
?.filter((obj) => obj._id === dataid)
.map((data) => setFormData({ ...formData, AddressTitle: data.AddressTitle }));
} catch (error) {
console.log(error);
}
};
api();
});
<TextField fullWidth id="outlined-basic" label="Address Title"
variant="outlined" size="small"
value={formData.AddressTitle}
onChange={(e) => { setFormData(formData => ({ ...formData,
AddressTitle: e.target.value }));
}} />
When I click edit, I see the correct value in the textfield, but I cannot change it. so I should be able to update the textfield I entered.Incoming data stays constant in the textfield, how can I update it?
Solution 1:[1]
dataValue is an array.
try this and tell me
const [formData, setFormData] = useState({
billingAddr: false,
shippingAddr: false,
AddressTitle: isEdit===true ? yourDisplayFunction() :("")
});
const yourDisplayFunction = (dataValue) => {
const dataValues = dataValue.map(value => {
return <>dataValue.AdressTitle</>
});
return <>dataValues</>
}
Solution 2:[2]
dataValue is an array of objects because [x, y, z].map(item => {item}) returns [ {x}, {y}, {z}]
So you are expecting a string from an array of objects. ("a" is not equivalent to dataValue)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jim2k |
| Solution 2 | EEAH |
