简体   繁体   English

如何在 React 中更新我的 state 数组而不更新 function

[英]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,我想将openingclosing时间从 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.

相关问题 我如何更新状态以使用 array.map 函数更新数组内对象的值 - how do i update state in react for updating the value of an object inside an array using array.map function 我无法在反应中更新阵列状态? - I cannot update my array state in react? 如何立即更新React状态? - How do I make my React state update immediately? React:如何从外部函数更新组件状态? - React: How do I update my components state from an external function? 我如何在React中正确更改元素的样式,而又不会在更新状态时使其不断变化? - How would I properly change the style of an element in React, without having it constantly change as I update state? 如何更新 state 与 axios 对我的 React 应用程序提出请求? (反应/节点快递) - How do I update state with axios put request on my React application? (React/Node Express) 在React with Redux中从我的商店/状态中删除项目后,如何更新视图 - How do I get my view to update after an item was removed from my store/State in React with Redux 我们如何在不删除状态域的情况下进行更新? (在React / JavaScript中) - How do we update without removing field of state? (in React / JavaScript) 如何更新 React 组件的数组? - How do I update an array for a React component? 如何在没有状态变量的情况下更新函数表达式? - How to update my function expression without state variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM