[英]How do I update my state array without having update function in React
I have a redux state array storeHoursCardRecord and state object storeDetails, I wanted to place the opening
and closing
time from storeDetails to storeHoursCardRecord's storeHoursTime array.我有一个 redux state 数组 storeHoursCardRecord 和 state object storeDetails,我想将
opening
和closing
时间从 storeDetails 放到 storeHoursCardRecord 的 storeHoursTime 数组中。
But on doing so I am using the push
method and whenever the component renders the values gets populated again and again in my storeHoursCardRecord's storeHoursTime.但是在这样做时,我使用的是
push
方法,每当组件呈现值时,就会在我的 storeHoursCardRecord 的 storeHoursTime 中一次又一次地填充值。
So is there any solution I can avoid it.那么有什么解决办法可以避免吗。 My initial state of storeHoursCardRecord is:
我的 storeHoursCardRecord 初始 state 是:
storeHoursCardRecord: [
{
day: "MONDAY",
storeHoursTime: [],
fullDayOpen: false
},
{
day: "TUESDAY",
storeHoursTime: [],
fullDayOpen: false
},
...
]
props.storeHoursCardRecord.map(idx => {
idx.storeHoursTime.push( // push method
props.storeDetails.storeHoursMap[idx.day].openingTime,
props.storeDetails.storeHoursMap[idx.day].closingTime
);
if (idx.storeHoursTime.join() === ["00:00", "23:59"].join()) {
// eslint-disable-next-line no-param-reassign
idx.fullDayOpen = true; // Can I avoid this assignment too.
}
return idx;
});
you can try你可以试试
return [...idx,{props.storeDetails.storeHoursMap[idx.day].openingTime,
props.storeDetails.storeHoursMap[idx.day].closingTime}]
You should create new objects in your map to avoid mutations:您应该在 map 中创建新对象以避免突变:
props.storeHoursCardRecord.map(idx => {
const { openingTime, closingTime } = props.storeDetails.storeHoursMap[idx.day];
return {
day: idx.day,
storeHoursTime: [openingTime, closingTime],
fullDayOpen: [openingTime, closingTime].join() === ["00:00", "23:59"].join(),
};
});
Then you will have a pure map, satisfying eslint.那么你就会得到一个纯map,满足eslint。 But depending of the context, you will have to either store the result in a local variable:
但是根据上下文,您必须将结果存储在局部变量中:
function YourComponent(props) {
const mappedStoreHoursCardRecord = props.storeHoursCardRecord.map(idx => {
const { openingTime, closingTime } = props.storeDetails.storeHoursMap[idx.day];
return {
day: idx.day,
storeHoursTime: [openingTime, closingTime],
fullDayOpen: [openingTime, closingTime].join() === ["00:00", "23:59"].join(),
};
});
// render mapped data here
return mappedStoreHoursCardRecord.map(record => <Card data={record} />);
}
Or get the state update function from the props:或者从道具中获取 state 更新 function:
function YourComponent(props) {
useEffect(() => {
props.setStoreHoursCardRecord(
props.storeHoursCardRecord.map(idx => {
const { openingTime, closingTime } = props.storeDetails.storeHoursMap[idx.day];
return {
day: idx.day,
storeHoursTime: [openingTime, closingTime],
fullDayOpen: [openingTime, closingTime].join() === ["00:00", "23:59"].join(),
};
});
}, []);
return /* ... */;
}
function ParentComponent() {
const [storeHoursCardRecord, setStoreHoursCardRecord] = useState(yourInitialState);
return (
<YourComponent
storeHoursCardRecord={storeHoursCardRecord}
setStoreHoursCardRecord={setStoreHoursCardRecord}
/* other props */
/>
);
}
create a copy using a spread operator使用传播运算符创建副本
props.storeHoursCardRecord.map(idx => {
const copy = {...idx}
copy.storeHoursTime.push(
props.storeDetails.storeHoursMap[idx.day].openingTime,
props.storeDetails.storeHoursMap[idx.day].closingTime
);
if (copy.storeHoursTime.join() === ["00:00", "23:59"].join()) {
// eslint-disable-next-line no-param-reassign
copy.fullDayOpen = true;
}
return copy;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.