I think it is weird, but I do not understand why useState does not being set to new value. When looking at the picture we could see useSelector is updated and it should update useState aswell.
I'm talking about flatData
state. From what I understand flatData`` should get
currentColivingdata at the first render. Then, whenever
currentColivingis updated -
flatData``` should be updated. However, the picture says different story.
Here is the code:
function FlatInformation() {
const currentColiving = useSelector(selectCurrentColiving);
const [imageFiles, setImageFiles] = useState([]);
const [flatData, setFlatData] = useState({
name: currentColiving?.title || "",
description: currentColiving?.description || "",
photos: currentColiving?.imageUrl || [],
amenities: currentColiving?.amenities || [],
});
// Boolean whether a person can edit coliving settings or not.
const flatSettingsEditMode = useSelector(selectIsInSettingsEditMode);
useEffect(() => {
if (currentColiving) {
console.log(flatData, 'flatData-begin');
setFlatData((prevState) => ({
name: currentColiving.title,
description: currentColiving.description,
photos: currentColiving.imageUrl,
amenities: currentColiving.amenities,
}));
console.log(currentColiving);
console.log(flatData, 'Flatdata-end');
}
}, [currentColiving]);
useEffect(() => {
console.log(flatData, "flatData");
}, [flatData]);
return (
<div>ommited</div>)
}