วิธีส่งอุปกรณ์ประกอบฉากไปยังส่วนประกอบที่ส่งผ่านไปยัง MenuItem ใน material-ui

ฉันมีรหัสต่อไปนี้เพื่อแสดงเมนูการแจ้งเตือน

      <Menu
        anchorEl={notificationAnchorEl}
        anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
        transformOrigin={{ vertical: 'top', horizontal: 'right' }}
        open={isNotificationMenuOpen}
        onClose={this.handleNotificationMenuClose}
      >
        {notifications.map(notification => (
          <MenuItem
            key={notification.id}
            component={NotificationItem}
            onClick={this.handleNotificationMenuClose}
          />
        ))}
      </Menu>

สิ่งที่ฉันไม่เข้าใจคือฉันจะส่งอุปกรณ์ประกอบฉากไปยังส่วนประกอบ NotificationItem โดยใช้วัตถุ notification ที่ฉันมีได้อย่างไร


person ManavM    schedule 14.02.2019    source แหล่งที่มา
comment
คุณช่วยอธิบายได้ไหมว่า MenuItem ทำอะไร หรือคุณต้องการให้มันทำอะไร   -  person cglacet    schedule 14.02.2019
comment
คุณใช้ Material UI เวอร์ชันใดอยู่   -  person darksmurf    schedule 14.02.2019
comment
MenuItem เป็นคอมโพเนนต์ Material-ui material-ui.com/api/menu-item   -  person ManavM    schedule 14.02.2019
comment
@ดาร์กสเมิร์ฟ 3.6.2   -  person ManavM    schedule 14.02.2019


คำตอบ (2)


เพียงแสดง NotificationItem ภายใน MenuItem หากนั่นคือสิ่งที่คุณต้องการ

นอกจากนี้ อย่าลืมส่ง key prop ที่ไม่ซ้ำกันไปยังแต่ละองค์ประกอบที่แมปจากอาร์เรย์

<Menu
    anchorEl={notificationAnchorEl}
    anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
    transformOrigin={{ vertical: 'top', horizontal: 'right' }}
    open={isNotificationMenuOpen}
    onClose={this.handleNotificationMenuClose}
>
    {notifications.map((notification, i) => (
        <MenuItem
           key={i}
           onClick={this.handleNotificationMenuClose}
        >
            <NotificationItem someProp={notification.someProp}/>
        </MenuItem>
    ))}
</Menu>
person darksmurf    schedule 14.02.2019
comment
นั่น..ผิด.. หากคุณดูที่เอกสารประกอบ ประเภทของอุปกรณ์ประกอบฉากคอมโพเนนต์คือ Component นอกจากนี้ยังมีการอธิบายส่วนประกอบที่ใช้สำหรับโหนดรูทด้วย สตริงที่จะใช้องค์ประกอบ DOM หรือ ส่วนประกอบ - person ManavM; 14.02.2019
comment
ถูกต้อง คุณสามารถใช้ส่วนประกอบได้หากต้องการ แต่คุณจะไม่สามารถส่งอุปกรณ์ประกอบฉากใด ๆ ไปให้องค์ประกอบนั้นได้ (ตามที่คำถามของคุณร้องขอ) ดังนั้นคุณควรวางส่วนประกอบไว้ใน MenuItem แทน - person darksmurf; 14.02.2019
comment
ขอขอบคุณที่ชี้ให้เห็น key prop อย่างไรก็ตาม ฉันอาจจะประสบปัญหาในภายหลัง - person ManavM; 14.02.2019
comment
ฉันพบวิธีการดังกล่าวใน การใช้งาน ListItem ของทุกสถานที่...จะตกลงไหมที่จะโพสต์คำตอบสำหรับคำถามของคุณเอง - person ManavM; 14.02.2019
comment
ฉันได้อัปเดตคำตอบเพื่อให้ชัดเจนยิ่งขึ้น หากคุณคิดว่าคุณมีคำตอบที่ดีกว่า คุณก็สามารถตอบคำถามของคุณเองได้ - person darksmurf; 14.02.2019
comment
แม้ว่าฉันจะยินดีเป็นอย่างยิ่งหากคุณลบสองบรรทัดแรกออกจากคำตอบเนื่องจากไม่ถูกต้องและฉันไม่ต้องการเผยแพร่ข้อมูลที่ผิด - person ManavM; 14.02.2019

หลังจากการค้นคว้า ฉันพบคำตอบใน การใช้งานองค์ประกอบ ListItem

ปรากฎว่าอุปกรณ์ประกอบฉากเพิ่มเติมทั้งหมดที่มอบให้กับ ListItem จะถูกส่งต่อไปยัง component

const {
  component: componentProp,
  ...other
} = props;

const componentProps = { className, disabled, ...other };
let Component = componentProp || 'li';

return (
  <ContainerComponent
    className={classNames(classes.container, ContainerClassName)}
    {...ContainerProps}
  >
    <Component {...componentProps}>{children}</Component>
    {children.pop()}
  </ContainerComponent>
);

↑ รหัสที่เกี่ยวข้องจาก ListItem.js

ดังนั้นโค้ดต่อไปนี้จะใช้งานได้

 {notifications.map(notification => (
   <MenuItem
     component={NotificationItem}
     onClick={this.handleNotificationMenuClose}
     notification={notification}
   />
  ))}
person ManavM    schedule 14.02.2019
comment
แม้ว่าจะใช้งานได้ แต่ก็คงจะดีถ้ามีคนอธิบายได้ว่าสิ่งนี้จะทำงานกับ typescript ได้อย่างไร เนื่องจากจะทำให้เกิดปัญหาในการพิมพ์หากฉันพยายามเพิ่ม NotificationItem อุปกรณ์ประกอบฉากใน MenuItem - person ManavM; 14.02.2019