简体   繁体   English

React 中 Arrays 的条件渲染:For vs Map

[英]Conditional Rendering of Arrays in React: For vs Map

I'm new to React and building a calendar application.我是 React 和构建日历应用程序的新手。 While playing around with state to try understand it better, I noticed that my 'remove booking' function required a state update for it to work, while my 'add booking' function worked perfectly without state. While playing around with state to try understand it better, I noticed that my 'remove booking' function required a state update for it to work, while my 'add booking' function worked perfectly without state.

Remove bookings: requires state to work删除预订:需要 state 才能工作

const [timeslots, setTimeslots] = useState(slots);
    

    const removeBookings = (bookingid) => {
        
        let newSlots = [...timeslots];
        delete newSlots[bookingid].bookedWith;
        setTimeslots(newSlots);
    }

Add bookings: does not require state to work添加预订:不需要 state 即可工作

   const addBookings = (slotid, tutorName) => {
        timeslots[slotid].bookedWith = tutorName;
      }

I think that this is because of how my timeslot components are rendered.我认为这是因为我的时间段组件是如何呈现的。 Each slot is rendered from an item of an array through.map(), as most tutorials online suggest is the best way to render components from an array.每个插槽都是通过.map() 从数组的一个项目中渲染出来的,因为大多数在线教程都建议这是从数组中渲染组件的最佳方式。

timeslots.map(slot => {
                
                if (!slot.bookedWith) {
                return <EmptyTimeslot [...props / logic] />
                } else {
                    return <BookedTimeslot [...props / logic]/> 
                }
                
                })

So, with each EmptyTimeslot, the data for a BookedTimeslot is available as well.因此,对于每个 EmptyTimeslot,BookedTimeslot 的数据也是可用的。 That's why state is not required for my add bookings function (emptyTimeslot -> bookedTimeslot).这就是为什么我的添加预订 function(emptyTimeslot ->bookedTimeslot)不需要 state 的原因。 However, removing a booking (bookedTimeslot -> emptyTimeslot) requires a rerender of the slots, since the code cannot 'flow upwards'.但是,删除预订 (bookedTimeslot -> emptyTimeslot) 需要重新渲染插槽,因为代码不能“向上流动”。

There are a lot of slots that have to be rendered each time.每次都必须渲染很多插槽。 My question is therefore, instead of mapping each slot (with both and information present in each slot), would it be more efficient to use a for loop to only render the relevant slot, rather than the information for both slots?因此,我的问题是,不是映射每个插槽(每个插槽中都存在和信息),而是使用 for 循环仅呈现相关插槽而不是两个插槽的信息会更有效吗? This I assume would require state to be used for both the add booking and remove booking function.我假设这需要 state 用于添加预订和删除预订 function。 Like this:像这样:

     for (let i=0;i<timeslots.length;i++) {
             if (!timeslot[i].bookedWith) {
                  return <EmptyTimeslot />
    } else {
                  return <BookedTimeslot />
}
    }

Hope that makes sense.希望这是有道理的。 Thank you for any help.感谢您的任何帮助。

Your addBooking function is bad .您的addBooking function不好 Even if it seems to "work", you should not be mutating your state values.即使它似乎“有效”,您也不应该改变您的 state 值。 You should be using a state setter function to update them, which is what you are doing in removeBookings .您应该使用 state 设置器 function 来更新它们,这就是您在removeBookings中所做的。

My question is therefore, instead of mapping each slot (with both and information present in each slot), would it be more efficient to use a for loop to only render the relevant slot, rather than the information for both slots?因此,我的问题是,不是映射每个插槽(每个插槽中都存在和信息),而是使用 for 循环仅呈现相关插槽而不是两个插槽的信息会更有效吗?

Your map approach is not rendering both.您的map方法不能同时渲染两者。 For each slot, it uses an if statement to return one component or the other depending on whether the slot is booked.对于每个插槽,它使用if语句返回一个组件或另一个组件,具体取决于插槽是否被预订。 I'm not sure how the for loop you're proposing would even work here.我不确定你提出的for循环如何在这里工作。 It would just return before the first iteration completed.它只会在第一次迭代完成之前return

This I assume would require state to be used for both the add booking and remove booking function.我假设这需要 state 用于添加预订和删除预订 function。

You should be using setTimeslots for all timeslot state updates and you should not be mutating your state values.应该所有时隙 state 更新使用setTimeslots ,并且您应该改变您的 state 值。 That is true no matter how you render them.无论您如何渲染它们都是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM