简体   繁体   中英

How to render a list of objects in React

Hi all : ) I'm trying to render a list of objects in reaction, but I can't replace whitespace with black text position (subtitle) . That checkbox box should not appear for the subTitle either.

在此处输入图片说明

A small sample of how this currently works

const dataFake = [
  { label: 'Aspiración', value: false },
  {
    subTitle: 'Fuego de las vías respiratorias'
  },
  {
    label: 'Tubo con manguito',
    value: false
  },
  { label: 'Tubo sin manguito', value: false },
  { label: 'Fi02 > 30%', value: false },
  {
    subTitle: 'No intubar'
  },
  { label: 'Esperado', value: false },
  { label: 'Inesperado', value: false },
  {
    subTitle: 'Manifest'
  }
];

{dataFake.map((item, index) => {
            return (
              <View key={index}>
                <Text style={{ fontStyle: 'italic', fontWeight: 'bold' }}>
                  {item.subTitle}
                </Text>
                <CheckBox
                  onClick={() => {}}
                  isChecked={item.value}
                  rightText={item.label}
                />
              </View>
            );
          })}

I would really appreciate any workaround for this to work 🙌

Use conditional rendering,


{dataFake.map((item, index) => {
            return (
              <View key={index}>
                {
                 item.subTitle !== undefined 
                 ? (
                <Text style={{ fontStyle: 'italic', fontWeight: 'bold' }}>
                  {item.subTitle}
                </Text>
                 )
                 : (
                  <CheckBox
                    onClick={() => {}}
                    isChecked={item.value}
                    rightText={item.label}
                  />
                 )
                }      
              </View>
            );
          })}

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