简体   繁体   中英

Loop through object, if condition is met, return additional mark up

I am looping through an object, it loops through the DropZone Object 4 times,

each DropZone has an id

dropZones: {
          'zone-1': {
            id: 'zone-1',
            title: 'zone-1',
            info: 'Strongest',
            items: [
              { id: 1, content: 'I am label 1' },
              { id: 2, content: 'I am label 2' },
            ],
            maxItems: 3,
            color: '#8bea97',
          },
          'zone-2': {
            id: 'zone-2',
            title: 'zone-2',
            info: 'Strong',
            items: [],
            maxItems: 5,
            color: '#bef7c6',
          },
          'zone-3': {
            id: 'zone-3',
            title: 'zone-3',
            info: 'Weakest',
            items: [{ id: 4, content: 'I am label 4' }],
            color: '#e6ffe9',
          },
          'start-zone': {
            id: 'start-zone',
            title: 'Inactive',
            info: 'Inactive',
            items: [
              { id: 5, content: 'I am label 5' },
              { id: 6, content: 'I am label 6' },
              { id: 7, content: 'I am label 7' },
              { id: 8, content: 'I am label 8' },
              { id: 9, content: 'I am label 9' },
            ],
            color: 'white',
          },
        },

For the 4th Dropzone, which has an id of start-zone , I want to render some JSX markup.

I check if the dropzone has the zoneId of start-zone , and then loop through it to render my markup.

if (zoneId === 'start-zone') {
                dropZone.items.map(item => (
                  <Placeholder>
                    <Title>Labels</Title>
                    <PlaceholderButton type="button" onClick={() => setShowInput(!showInput)}>
                      +
                    </PlaceholderButton>
                    <Item
                      draggable
                      onDrag={() => {
                        setDragSource(dropZone.id)
                        setDraggedItem(item)
                      }}
                      key={item.id}
                    >
                      {item.content}
                    </Item>
                  </Placeholder>
                ))
              }

My Item 's are rendering just fine. However <Title /> , PlaceholderButton is not being rendered in my UI.

Heres the full loop

<div onDrop={onDrop}>
        {Object.keys(currentAnswer).length !== 0
          ? zoneOrder.map(zoneId => {
              const dropZone = currentAnswer[zoneId]
              if (zoneId === 'start-zone') {
                dropZone.items.map(item => (
                  <Placeholder>
                    <Title>Labels</Title>
                    <PlaceholderButton type="button" onClick={() => setShowInput(!showInput)}>
                      +
                    </PlaceholderButton>
                    <Item
                      draggable
                      onDrag={() => {
                        setDragSource(dropZone.id)
                        setDraggedItem(item)
                      }}
                      key={item.id}
                    >
                      {item.content}
                    </Item>
                  </Placeholder>
                ))
              }
              return (
                <DropZone
                  onDragOver={event => onDragOver(event)}
                  data-droppable
                  id={dropZone.id}
                  key={dropZone.id}
                  onDragLeave={onDragLeave}
                >
                  {dropZone.items.map(item => (
                    <Item
                      draggable
                      onDrag={() => {
                        setDragSource(dropZone.id)
                        setDraggedItem(item)
                      }}
                      key={item.id}
                    >
                      {item.content}
                      <DropZoneLabel>{dropZone.title}</DropZoneLabel>
                    </Item>
                  ))}
                </DropZone>
              )
            })
          : null}

There's no errors in my console to indicate why this would happen. Is there a better way to render specfic mark up for my DropZone ?

I think the problem is that you're not saving the results of the .map() and passing it to render.

Inside your if, you do

 if (zoneId === 'start-zone') {
                dropZone.items.map(item => (
  ...
)

but you don't assign it to a variable, so the resulting JSX is thrown away.

try

let result;
if (zoneId === 'start-zone') {
   result = dropZone.items.map(item => (
     ...
   )
}


return (
                <DropZone
                  onDragOver={event => onDragOver(event)}
                  data-droppable
                  id={dropZone.id}
                  key={dropZone.id}
                  onDragLeave={onDragLeave}
                >
                  {result}
                </DropZone>
) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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